2012-03-08 33 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 2012-03-08 17:55:03

+0

我的問題是,應用程序無法檢索「firstName」,我可以看到有一些數據交換正在進行,但我的應用程序不會在最後顯示任何內容。 – 2012-03-08 18:00:08

+0

將日誌放入您的應用中以查看發生了什麼。 「Log.d(」YourTag「,」Test hit this line「);' – Blundell 2012-03-08 18:36:34

回答

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,謝謝!它的工作正常!沒有你的幫助,我無法做到。 – 2012-03-08 18:40:53

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 
    } 

} 

這些方法將有助於從服務器獲取響應

/** 
* 
* @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(); 
} 

JSON響應的例子中,你送的是:

{ 
    "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

嘿穆罕默德,非常感謝你的迴應!我真的很感激。我現在就試一試,讓你知道它是如何發展的。 – 2012-03-08 18:33:47

+0

穆罕默德,你能告訴我如何解析json嗎?感謝你的幫助。 – 2012-03-13 20:46:43

+0

穆罕默德,你能幫助我嗎? – 2012-03-15 20:42:54