2012-03-08 53 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