解析dataorg.json.JSONException时出错:java.lang.String类型的值<br无法转换为JSONObject

i'm trying to make android food order for my thesis and because this error i'm running out of time :(

error on logcat :

Error parsing dataorg.json.JSONException: Value cannot be converted to JSONObject org.json.JSONException: Value to JSONObject

here's my JSONParser :

package com.makanan.restotradisional;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

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

public JSONParser() {
}

// fungsi abil json url lewat method HTTP POST atau GET
public JSONObject makeHttpRequest(String url, String method,
        List<NameValuePair> params) {
    try {
        if (method == "POST") {
            // jika request method adalah 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") {
            // jika request method adalah 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 + "
");
        }
        is.close();
        json = sb.toString();

    } catch (Exception e) {

        Log.e("Buffer Error", "Error Converting result" + e.toString());
    }



    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data" + e.toString());
        e.printStackTrace();
    }
    return jObj;

}
}

this my PHP & Java : http://www.4shared.com/rar/1lGplX19ba/Java_and_PHP.html

and this is my database on phpmyadmin :http://www.4shared.com/rar/y_UMtL7_ce/rumah_makan.html

please help me

drno94939847
drno94939847 请提供您在JSONObject构造函数中传递的String
大约 5 年之前 回复

3个回答

Remove any of the <br> statements or echo statements from your php file except the one that you are using to pass json..

Check the output of your file in browser, remove all the unwanted things other than json..



请打印并检查字符串 json </ code>的格式是否正确 JSONObject < / code>构造函数。 根据文档,构造 JSONObject </ code>的有效json字符串应为 - {</ code>(左括号)开头并以}结尾的字符串</ code> (右支撑)。</ strong> </ p>

请参考这个。</ p>
</ div>

展开原文

原文

Please print and check if your string json is in right format as expected by the JSONObject constructor. Per documentation, valid json string to construct JSONObject should be -- A string beginning with { (left brace) and ending with } (right brace).

Please refer this.



进入JSONParser并执行此操作以便在logcat中看到什么来自php。 可能是一个php错误。</ p>

  try {
jObj = new JSONObject(json);
} catch(JSONException e){
//这一行就是你的意思 需要添加
Log.d(“Whats wrong?”,json.toString());
Log.e(“JSON Parser”,“解析数据时出错”+ e.toString());
} \ n </ code> </ pre>
</ div>

展开原文

原文

Go inside JSONParser and do this so u see in logcat whats comming from php. Probably its a php error.

try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        //This line is what u need to add
        Log.d("Whats wrong?", json.toString());
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐