将字符串转换为JSONObject

时间:2014-05-04 14:58:08

标签: php android json

我正在尝试将数据从我的Android应用程序发送到服务器,但是当我尝试运行我的应用程序时,它会与服务器连接,但过了一段时间后,错误会停止我的应用程序。

错误是:

  

“05-04 19:39:36.401:E / JSON Parser(2323):解析数据时出错org.json.JSONException:java.lang.String类型的值abc@yahoo.com无法转换为JSONObject”< / p>

在下面的代码....请检查最后try-catch .... 如果有人有任何想法,请帮助...

public class JSONParser {static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
        List<NameValuePair> params) {

    // Making HTTP request
    try {

        // check for request method
        if(method == "POST"){
            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        }else if(method == "GET"){
            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }           


    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        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
    return jObj;

}

}

2 个答案:

答案 0 :(得分:0)

这可能是因为你试图转换字符串&#34; abc@yahoo.com"到JSONObject。您需要String为JSON格式。类似的东西:

{
    "email":"abc@yahoo.com"
}

答案 1 :(得分:0)

如果您正在阅读一个简单的字符串值,例如电子邮件,请说 abc@yahoo.com ,那么您应首先创建JSONObject,并在其中设置键/值对

try {
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            is, "iso-8859-1"), 8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    is.close();
    json = sb.toString();
    **jObj = new JSONObject();**
    **jObj.putString("email", json);**

} catch (Exception e) {
    Log.e("Buffer Error", "Error converting result " + e.toString());
}

使用部分原始代码编辑的答案 现在,您已经创建了自己的JSONObject,以防服务器没有返回一个。 希望它有所帮助。

相关问题