2017-03-09 15 views
-1

MySQLからデータをフェッチしてtextviewに表示したいとします。 "org.json.JSONArray型のValue []がJSONObjectに変換できない"というログcatのエラーが発生しました。単一アクティビティで動作しますが、インテントではデータをフェッチしません。すべてのためのVolleyを使用してMySQLからデータをフェッチする際にエラーが発生しました。

MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

private EditText editTextId,ed1; 
private Button buttonGet; 




@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    editTextId = (EditText) findViewById(R.id.editTextId); 
    buttonGet = (Button) findViewById(R.id.buttonGet); 

    buttonGet.setOnClickListener(this); 

} 



@Override 
public void onClick(View v) { 

    Intent i =new Intent(MainActivity.this,SecondActivity.class); 
    startActivity(i); 
} 
} 

SecondActivity

public class SecondActivity extends AppCompatActivity { 

private TextView textViewResult,tv,tv2,tv3,tv4,tv5,tv6; 
private ProgressDialog loading; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_second); 

    textViewResult = (TextView) findViewById(R.id.textViewResult); 
    tv = (TextView) findViewById(R.id.textView); 
    tv2 = (TextView) findViewById(R.id.textView2); 
    tv3 = (TextView) findViewById(R.id.textView3); 
    tv4 = (TextView) findViewById(R.id.textView4); 
    tv5 = (TextView) findViewById(R.id.textView5); 
    tv6 = (TextView) findViewById(R.id.textView6); 
    getData(); 
} 

private void getData() { 
    // String id = editTextId.getText().toString().trim(); 

// if (id.equals("")) { 
    // Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show(); 
    // return; 
// } 
    loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false); 

    String url = Config.DATA_URL;//+editTextId.getText().toString().trim(); 
    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      loading.dismiss(); 
      showJSON(response); 
     } 
    }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Toast.makeText(SecondActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show(); 
       } 
      }); 

    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(stringRequest); 
} 

private void showJSON(String response){ 
    String n=""; 
    String p2o5=""; 
    String k2o = ""; 
    String urea = ""; 
    String phos = ""; 
    String potash = ""; 
    try { 
     /**/ 

     JSONObject jsonObject = new JSONObject(response); 
     Log.e("response",""+jsonObject); 
     JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY); 
     for(int i=0;i<result.length();i++) { 
      JSONObject collegeData = result.getJSONObject(i); 
      n = collegeData.getString(Config.KEY_N); 
      p2o5 = collegeData.getString(Config.KEY_P2o5); 
      k2o = collegeData.getString(Config.KEY_k2o); 
      urea = collegeData.getString(Config.KEY_urea); 
      phos = collegeData.getString(Config.KEY_phos); 
      potash = collegeData.getString(Config.KEY_potash); 
     } } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    // textViewResult.setText("Name:\t"+name+"\nEmail:\t" +address+ "\nAddress:\t"+ vc); 
    // textViewResult.setText("Name:\t"+name); 
    textViewResult.setText("N:\t"+n); 
    tv.setText("P2O5:\t"+p2o5); 
    tv2.setText("K2O:\t"+k2o); 
    tv4.setText("Urea:\t"+urea); 
    tv5.setText("Phosphate:\t"+phos); 
    tv6.setText("Potash :\t\t\t\t\t" +potash); 
} 
} 

コンフィグ

public static final String DATA_URL = "http://........ag.php?id="; 
public static final String KEY_N = "n"; 
public static final String KEY_P2o5 = "p2o5"; 
public static final String KEY_k2o = "k2o"; 
public static final String KEY_urea = "urea"; 
public static final String KEY_phos = "phos"; 
public static final String KEY_potash = "potash"; 
public static final String JSON_ARRAY = "agri_result_1"; 
+0

あなたのJSONレスポンスを投稿... – user2025187

+0

私は応答を取得できませんでした。 – Jincy

+0

コードサンプルを減らしてエラーを特定し、発生したエラーとその発生場所を正確に表示できるかどうかを確認してください。 –

答えて

0

おかげで、私は答え、その完璧に働いたのです。

主な活動

public void OnClick(View V){ 
Intent intent = new Intent(MainActivity.this, SecondActivity.class); 
      intent.putExtra("id",ed1.getText().toString()); 
      startActivity(intent); 
} 

セカンド活動

private void getData() { 

    String id = getIntent().getExtras().getString("id"); 
    ading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false); 


    String url = "http://...../ooo.php?id="; 

    StringRequest stringRequest = new StringRequest(url, new  Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      loading.dismiss(); 
      showJSON(response); 
     } 
    }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Toast.makeText(SecondActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show(); 
       } 
      }); 

    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(stringRequest); 
} 
関連する問題