2012-03-08 15 views
0

私は過去4日間これを把握しようとしていましたが、私は髪を引き出しようとしています!JSONを解析できませんアプリケーションでは

私はあなたの助けが必要です、誰かが間違って何を言っているのですか?ここで

は、私が開発に使用していますJSON形式のリンクです:プライバシーのために取り外さここ

に関する私のコードです:

public class JSONActivity extends Activity { 


TextView http; 
HttpClient client; 
JSONObject json; 


final static String URL = "REMOVED FOR PRIVACY CONCERNS 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    http = (TextView) findViewById(R.id.http); 
    client = new DefaultHttpClient(); 
    new Read().execute("firstName"); 


} 

public JSONObject getpw(String password) 
     throws ClientProtocolException, IOException, JSONException { 
    StringBuilder url = new StringBuilder(URL); 
    url.append(password); 

    HttpGet get = new HttpGet(url.toString()); 
    HttpResponse r = client.execute(get); 
    int status = r.getStatusLine().getStatusCode(); 
    if (status == 200) { 
     HttpEntity e = r.getEntity(); 
     String data = EntityUtils.toString(e); 
     JSONArray getname = new JSONArray(data); 
     JSONObject last = getname.getJSONObject(3); 
     return last; 
    } else { 
     Toast.makeText(JSONActivity.this, "error", Toast.LENGTH_SHORT); 
     return null; 
    } 
} 
public class Read extends AsyncTask<String, Integer, String>{ 

    @Override 
    protected String doInBackground(String... arg0) { 
     // TODO Auto-generated method stub 
     try { 
      json=getpw("trustme"); 
      return json.getString(arg0[0]); 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     // TODO Auto-generated method stub 
     http.setText(result); 
    } 

}} 

任意の助けいただければ幸いです。

+1

あなたの問題が何であるかを説明するために非常に大きな助けをしています... – andreapier

+0

私の問題は、アプリケーションが "firstName"を取得するのに失敗し、何らかのデータ交換が行われているのがわかります。 –

+0

あなたのアプリにログを書き込んで、何が起こっているのかを確認します。 Logged( "YourTag"、 "Test hit this line"); ' – Blundell

答えて

2

あなたはJSONArrayに戻って期待しているが、WebサービスだけですJSONObjectを返します。

私は次のように変更作ら:

JSONArray getname = new JSONArray(data); - >JSONObject getname = new JSONObject(data);

をし、非同期タスクでは、私は次の変更作ら:

return json.getString("firstName");

をここで完全なコードがあります

public class PlaygroundActivity extends Activity { 
    TextView http; 
    HttpClient client; 
    JSONObject json; 

    final static String URL = "REMOVED FOR PRIVACY CONCERNS 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     http = (TextView) findViewById(R.id.http); 
     client = new DefaultHttpClient(); 
     new Read().execute("firstName"); 

    } 

    public JSONObject getpw(String password) throws ClientProtocolException, 
      IOException, JSONException { 
     StringBuilder url = new StringBuilder(URL); 
     url.append(password); 

     HttpGet get = new HttpGet(url.toString()); 
     HttpResponse r = client.execute(get); 
     int status = r.getStatusLine().getStatusCode(); 
     if (status == 200) { 
      HttpEntity e = r.getEntity(); 
      String data = EntityUtils.toString(e); 
      JSONObject getname = new JSONObject(data); 

      return getname; 
     } else { 
      Toast.makeText(PlaygroundActivity.this, "error", Toast.LENGTH_SHORT); 
      return null; 
     } 
    } 

    public class Read extends AsyncTask<String, Integer, String> { 

     @Override 
     protected String doInBackground(String... arg0) { 
      // TODO Auto-generated method stub 
      try { 
       json = getpw("trustme"); 
       return json.getString("firstName"); 
      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      // TODO Auto-generated method stub 
      http.setText(result); 
     } 
    } 
} 
+0

QuinnVT、ありがとう!そのうまくいく!私はあなたの助けなしにそれをしなかった。 –

0

次のコードを使用してサーバーから応答を取得し、jsonを解析できます。

public static void extractParameters(){ 
    String urlRequest = "REMOVED FOR PRIVACY CONCERNS"; 
    String response = getServerResponse(urlRequest); 
    try { 
     JSONObject json = new JSONObject(response); 
     boolean suspended = json.getBoolean("suspended"); 
     String firstName = json.getString("firstName"); 
     String lastName = json.getString("lastName"); 
     int checkdin = json.getInt("checkedin"); 
     int checkindatetime = json.getInt("checkindatetime"); 
     //Get Json object address 
     JSONObject address = json.getJSONObject("address"); 
     String streedAddress = address.getString("streetAddress");//In same way get city etc 
     //Get PhoneNumber Array 
     JSONArray phoneNumbers = json.getJSONArray("phoneNumber"); 
     String type = phoneNumbers.getJSONObject(0).getString("type"); 
     String number = phoneNumbers.getJSONObject(0).getString("number");// and so on for 1,2,3... 


    } catch (Exception e) { 
     // TODO: handle exception 
    } 

} 

これらのメソッドは、サーバーからの応答を取得するのに役立ちますあなたが送信されたJSONレスポンスの

/** 
* 
* @param urlRequest URL of server 
* @return 
*/ 
public static String getServerResponse(String urlRequest){ 
    Log.d("urlRequest",urlRequest); 
    String response = ""; 
    HttpURLConnection conn = null; 
    try { 
     conn = (HttpURLConnection) new URL(urlRequest).openConnection(); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try { 
     response = read(conn.getInputStream()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    Log.d("response",response); 
    return response.trim(); 
} 
private static String read(InputStream in) throws IOException { 
    StringBuilder sb = new StringBuilder(); 
    BufferedReader r = new BufferedReader(new InputStreamReader(in), 1000); 
    for (String line = r.readLine(); line != null; line = r.readLine()) { 
     sb.append(line); 
    } 
    in.close(); 
    return sb.toString(); 
} 

例は次のとおりです。

{ 
    "suspended": "false", 
    "firstName": "John", 
    "lastName": "Smith", 
    "checkedin": 0, 
    "checkindatetime": 0, 
    "address": { 
    "streetAddress": "21 2nd Street", 
    "city": "New York", 
    "state": "NY", 
    "postalCode": "10021" 
    }, 
    "phoneNumber": [ 
    { 
     "type": "home", 
     "number": "212 111-1111" 
    }, 
    { 
     "type": "fax", 
     "number": "646 222-2222" 
    } 
    ] 
} 
+0

Hey Muhammad、ありがとう、あなたの応答に非常に感謝します!私は本当にそれを感謝します。私は今それを試して、それがどのように行くかを知らせます。 –

+0

Muhammad、後でjsonを解析する方法を教えてもらえますか?あなたのすべての協力に感謝します。 –

+0

私をさらに助けてくれるでしょうか、ムハンマド? –

関連する問題