发送数据的JSON异常

时间:2014-07-05 20:09:15

标签: android json apache

错误:

Value <html><head><title>Apache of type java.lang.String cannot be converted to JSONObject 
org.json.JSONException: Value <html><head><title>Apache of type java.lang.String cannot be converted to JSONObjectat org.json.JSON.typeMismatch(JSON.java:111)
at org.json.JSONObject.<init>(JSONObject.java:158)
at org.json.JSONObject.<init>(JSONObject.java:171)
at com.avilyne.android.MainActivity.handleResponse(MainActivity.java:107)
at com.avilyne.android.MainActivity$WebServiceTask.onPostExecute(MainActivity.java:216)
at com.avilyne.android.MainActivity$WebServiceTask.onPostExecute(MainActivity.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

代码:

public void handleResponse(String response) 
{     
    EditText edFirstName = (EditText) findViewById(R.id.first_name);
    EditText edLastName = (EditText) findViewById(R.id.last_name);
    EditText edEmail = (EditText) findViewById(R.id.email);

    String firstName=edFirstName.getText().toString();
    String lastName=edLastName.getText().toString();
    String email=edEmail.getText().toString();

    edFirstName.setText("");
    edLastName.setText("");
    edEmail.setText("");

    try 
    {

        JSONObject jso = new JSONObject(response.toString());

        String jfirstName = jso.getString(firstName);
        String jlastName = jso.getString(lastName);
        String jemail = jso.getString(email);

        edFirstName.setText(jfirstName);
        edLastName.setText(jlastName);
        edEmail.setText(jemail);                      
    } catch (Exception e) 
    {
        Log.e(TAG, e.getLocalizedMessage(), e);
    }    
}

1 个答案:

答案 0 :(得分:0)

日志告诉你原因:

Value <html><head><title>Apache of type java.lang.String cannot be converted to JSONObject

剥离html标记以获取原始Json字符串,并将其传递给JSONObject构造函数。


Raw Json字符串如下所示:

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
        }
    }
}