getText.toString()无法转换为JSONObject

时间:2014-06-03 04:45:28

标签: android json

我有一个正确显示facebook id的TextView。

当我尝试从中获取http请求的文本时,我得到 java.lang.String类型的值无法转换为JSONObject 。例如:

fbID = fbidTextView.getText().toString();

如果我手动为fbID分配值,则该请求不会出错。例如:

fbID = "12345678910112";

我在第一段代码片段中做的事情有什么明显错误吗?

我尝试了很多东西而且我很难过。

谢谢你的时间!

编辑添加了CheckUser AsyncTask

抱歉,错过了这一点:

        new CheckUser().execute(fbID);



class CheckUser extends AsyncTask<String, String, String> {

    Bundle extras = getIntent().getExtras();
    /**
     * Checking if user has a username yet
     * */
    protected String doInBackground(String... args) {

        //String fbid = fbID;
        String fbid = args[0];

        Log.d("Arg fb: ", fbid);

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("fbid", fbid));

        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_check_user, "POST", params);

再次编辑,自定义JSON解析器类:

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;

}
}

logcat的:

06-02 22:58:42.953: E/JSON Parser(23462): Error parsing data org.json.JSONException: Value You of type java.lang.String cannot be converted to JSONObject
06-02 22:58:42.953: E/AndroidRuntime(23462): FATAL EXCEPTION: AsyncTask #1
06-02 22:58:42.953: E/AndroidRuntime(23462): Process: com.example.androidhive, PID: 23462
06-02 22:58:42.953: E/AndroidRuntime(23462): java.lang.RuntimeException: An error occured while executing doInBackground()
06-02 22:58:42.953: E/AndroidRuntime(23462):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
06-02 22:58:42.953: E/AndroidRuntime(23462):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
06-02 22:58:42.953: E/AndroidRuntime(23462):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
06-02 22:58:42.953: E/AndroidRuntime(23462):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
06-02 22:58:42.953: E/AndroidRuntime(23462):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-02 22:58:42.953: E/AndroidRuntime(23462):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-02 22:58:42.953: E/AndroidRuntime(23462):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-02 22:58:42.953: E/AndroidRuntime(23462):    at java.lang.Thread.run(Thread.java:841)
06-02 22:58:42.953: E/AndroidRuntime(23462): Caused by: java.lang.NullPointerException
06-02 22:58:42.953: E/AndroidRuntime(23462):    at com.example.androidhive.MainActivity$CheckUser.doInBackground(MainActivity.java:209)
06-02 22:58:42.953: E/AndroidRuntime(23462):    at com.example.androidhive.MainActivity$CheckUser.doInBackground(MainActivity.java:1)
06-02 22:58:42.953: E/AndroidRuntime(23462):    at android.os.AsyncTask$2.call(AsyncTask.java:288)

MainActivity 209行是这样的:

int success = json.getInt(TAG_SUCCESS);

出于某种原因,我使用 fbidTextView.getText()。toString(); json变为空?

2 个答案:

答案 0 :(得分:0)

以下是将String(fbid)分配给JSON对象

的方法
String id = fbidTextView.getText().toString();

JSONObject json=new JSONObject("{\"fbID\":"+ id +"}");

答案 1 :(得分:0)

这是问题,

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("fbid", fbid));

    // getting JSON Object
    // Note that create product url accepts POST method
    JSONObject json = jsonParser.makeHttpRequest(url_check_user, "POST", params);

您使用了param作为列表。但它应该是JsonObject 使用这个,

JSONObject params = new JSONObject();
params.put(("fbid", fbid);
JSONObject json = jsonParser.makeHttpRequest(url_check_user, "POST", params);