2015-07-02 124 views
-5

我正在尝试为我的论文制作android食品订单,并且因为这个错误我将耗尽时间: (解析dataorg.json.JSONException错误:java.lang.String类型的值不能转换为JSONObject

错误的logcat的:

错误解析dataorg.json.JSONException:值不能被转换为JSONObject的 org.json.JSONException:值以JSONObject的

这里是我的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 + "\n"); 
     } 
     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; 

} 
} 

这是我的PHP &的Java:http://www.4shared.com/rar/1lGplX19ba/Java_and_PHP.html

,这是我在phpMyAdmin的数据库:http://www.4shared.com/rar/y_UMtL7_ce/rumah_makan.html

请帮我

+0

请提供您在'JSONObject'构造函数中传递的'String' –

回答

0

删除任何<br>陈述或回声来自你的php文件的声明,除了你用来传递json的文件..

入住浏览器中文件的输出,删除所有比JSON其他不必要的东西..

0

请打印并检查你的字符串json是正确的格式由JSONObject构造预期。根据文档,有效的json字符串构造JSONObject应该是 - {(左大括号)开头并以}(右大括号)结尾的字符串。

请参考this

0

进入JSONParser并做到这一点,所以你看到在logcat什么来自PHP。可能它是一个PHP错误。

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()); 
    } 
相关问题