如何从Android发布JSON数据?

时间:2013-02-07 11:59:57

标签: android json

IN JSON在Android中进行解析,因为我想在http URL上发布以下格式的数据,

 webdata={"email":"test@test.com","password":"123456"}  

如何通过在http url上使用httppost为此JSON发布此数据?

3 个答案:

答案 0 :(得分:1)

首先你可以准备你的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 :(得分: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;
}

之后使用以下方法在网址上发布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("test@test.com",
                        "123456"));

现在您可以根据需要轻松反序列化jsonObject。

注意:您的帖子数据必须是Json对象。正如我所见,webdata = {“email”:“test@test.com”,“password”:“123456”}不是jsonObject这里json对象只有{“email”:“test@test.com”,“密码“:”123456“}

答案 2 :(得分:0)

使用StringEntity课程。

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

然后将其添加到您的HttpPost

httpPost.setEntity(entity);