2015-07-02 171 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()); 
    } 
相關問題