2017-03-08 5 views
-1

をjava.lang.Stringでには適用できません。JSONArrayは、私は、このボレーonResponseを持って

public void onResponse(String response) { 
    try { 
     JSONArray info = new JSONArray(response); 

     String name = info.getString("name"); 
     String picture = info.getString("picture"); 
     Picasso.with(context).load(picture).into(profile); 
     user_name.setText(name); 


    } catch (JSONException e) { 
     e.printStackTrace(); 
     Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show(); 
    } 

} 

問題が("name")("picture")がJSONArrayはjava.lang.Stringでに適用することができない私を与えることです。

私は何を欠席しましたか?

編集:

[{"name":"josh","picture":"http:\/\/192.168.0.11\/pic.png"}] 
+1

レスポンスを共有できますか?名前とピクチャの値がJSONArrayで、文字列だけではないようです。 – Shubham

+0

@Shubhamそれは次のようなものです: '[{" name ":" josh "、" picture ":" http:///ip//pic.png "}]' –

+3

あなたは配列上でループしていません! –

答えて

2

この応答を解析することをしなさい!

try { 
      JSONArray info = new JSONArray(response); 

      for (int i =0; i<info.length() ; i++) { 
       JSONObject obj = info.getJSONObject(i); 
       String name = obj.getString("name"); 
       String picture = obj.getString("picture"); 
       Picasso.with(context).load(picture).into(profile); 
       user_name.setText(name); 
      } 


     } catch (JSONException e) { 
      e.printStackTrace(); 
      Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show(); 
     } 
+0

あなたの答えをありがとう! –

+0

@RickJoe you'er welcome –

1

@Atefノウサギのコメントに拡大:

JSONArray info = new JSONArray(response); 

String name = info.getJSONObject(0).getString("name"); 
String picture = info.getJSONObject(0).getString("picture"); 
Picasso.with(context).load(picture).into(profile); 
user_name.setText(name); 
+0

ありがとうございました –

+0

指摘していただきありがとうございます。 – Shubham

1

変化にコードの下に。

try { 
    JSONArray info = new JSONArray(response); 

    String name = info.getJSONObject(0).getString("name"); 
    String picture = info.getJSONObject(0).getString("picture"); 
    Picasso.with(context).load(picture).into(profile); 
    user_name.setText(name); 


} catch (JSONException e) { 
    e.printStackTrace(); 
    Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show(); 
} 
+0

ありがとう、お友達。 –

関連する問題