2013-02-07 71 views
0

在JSON解析在Android作爲我希望與後下方HTTP URL格式的數據,如何從android發佈JSON數據?

webdata={"email":"[email protected]","password":"123456"} 

我怎樣才能發佈通過使用httppost的這個JSON的HTTP URL這些數據?

+0

你想發佈總JSON字符串或只是電子郵件和密碼? – Vamshi

+0

試試這個 http://stackoverflow.com/questions/3027066/how-to-send-a-json-object-over-request-with-android – Parvathy

回答

0

使用StringEntity類。

StringEntity entity = new StringEntity("Your JSON String",HTTP.UTF_8); 

,然後將其添加到您的HttpPost

httpPost.setEntity(entity); 
0

首先,你可以準備JSON格式的數據,然後發送,隨着你的要求按您的要求GET或POST這需要被用做的AsyncTask

JSONObject jObject = new JSONObject(); 
try{ 
jObject.put("email",urvalue); 
jObject.put("password",urvalue); 

List<NameValuePair> pairs = new ArrayList<NameValuePair>(); 
pairs.add(new BasicNameValuePair("webdata", jObject.toString())); 

DefaultHttpClient httpClient = new DefaultHttpClient(); 
HttpPost httpPost = new HttpPost(url); 
httpPost.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8")); 
HttpResponse httpResponse = httpClient.execute(httpPost); 
HttpEntity httpEntity = httpResponse.getEntity(); 
is = httpEntity.getContent(); 

}catch(Exception e){} 
1

使用以下用於產生JSON對象方法

private JSONObject getConvertedinJson(String email, String password) { 

    JSONObject object = new JSONObject(); 
    try { 
     object.put("email", email); 
     object.put("password", password); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return object; 
} 
使用後下列關於URL發佈JSON對象

public JSONObject getJSONFromUrl(String url, JSONObject jObj) { 

    // Making HTTP request 
    try { 
     // Default Http Client 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     // Http Post Header 
     HttpPost httpPost = new HttpPost(url); 
     StringEntity se = new StringEntity(jObj.toString()); 
     httpPost.setEntity(se); 
     httpPost.setHeader("Accept", "application/json"); 
     httpPost.setHeader("Content-type", "application/json"); 
     // Execute Http Post Request 
     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     is = httpEntity.getContent(); 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    /* 
    * To convert the InputStream to String we use the 
    * BufferedReader.readLine() method. We iterate until the BufferedReader 
    * return null which means there's no more data to read. Each line will 
    * appended to a StringBuilder and returned as String. 
    */ 
    try { 
     // Getting Server Response 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     // Reading Server Response 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     json = sb.toString(); 
    } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 

    // try parse the string to a JSON object 
    try { 
     jObj = new JSONObject(json); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

    // return JSON String 
    Log.e("JSON Parser", jObj.toString()); 
    return jObj; 

} 

現在使用下面的語句來獲得JSON響應方法

JSONObject jsonObject = getJSONFromUrl(
       "www.url.com/api", 
       getConvertedinJson("[email protected]", 
         "123456")); 

現在您可以根據需要輕鬆地反序列化jsonObject。

注意:您的發佈數據必須是Json對象。正如我所看到的webdata = {「email」:「[email protected]」,「password」:「123456」}不是jsonObject這裏json對象只有{「email」:「[email protected]」,「password 「:」123456「}