Android 获取错误:解析数据org.json.JSONException:Value时出错<;无法将java.lang.String类型的br转换为JSONObject
我的json解析器中有以下代码。 我尝试过从iso-8859-1改为utf-8。 但我总是犯这样的错误。我做错了什么?? 我不知道我做错了什么Android 获取错误:解析数据org.json.JSONException:Value时出错<;无法将java.lang.String类型的br转换为JSONObject,android,mysql,Android,Mysql,我的json解析器中有以下代码。 我尝试过从iso-8859-1改为utf-8。 但我总是犯这样的错误。我做错了什么?? 我不知道我做错了什么 package com.iwantnew.www; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedE
package com.iwantnew.www;
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 = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET method
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;
}
}
package.com.iwantnew.www;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.io.UnsupportedEncodingException;
导入java.util.List;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.client.utils.URLEncodedUtils;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.util.Log;
公共类JSONParser{
静态InputStream为空;
静态JSONObject jObj=null;
静态字符串json=“”;
//建造师
公共JSONParser(){
}
//函数从url获取json
//通过使用HTTP POST或GET方法
公共JSONObject makeHttpRequest(字符串url、字符串方法、,
列表参数){
//发出HTTP请求
试一试{
//检查请求方法
如果(方法==“POST”){
//请求方法为POST
//defaultHttpClient
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}else if(方法==“GET”){
//请求方法是GET
DefaultHttpClient httpClient=新的DefaultHttpClient();
String paramString=URLEncodedUtils.format(params,“utf-8”);
url+=“?”+参数字符串;
HttpGet HttpGet=新的HttpGet(url);
HttpResponse HttpResponse=httpClient.execute(httpGet);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
is,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
json=sb.toString();
}捕获(例外e){
Log.e(“缓冲区错误”,“错误转换结果”+e.toString());
}
//尝试将字符串解析为JSON对象
试一试{
jObj=新的JSONObject(json);
}捕获(JSONException e){
Log.e(“JSON解析器”,“错误解析数据”+e.toString());
}
//返回JSON字符串
返回jObj;
}
}
我的数据库使用了mysql。我对安卓数据库还不熟悉。救命啊!
我的php文件如下所示:
<?php
/*
* Following code will create a new product row
* All product details are read from HTTP Post Request
*/1
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['quantity']) && isset($_POST['price']) && isset($_POST['descriptions'])) {
//$location = $_POST['location'];
$quantity = $_POST['quantity'];
$price = $_POST['price'];
//$productID = $_POST['area'];
$contact = $_POST['contact'];
$descriptions = $_POST['descriptions'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO room_tb(quantity, price,description) VALUES('$quantity', '$price','$descriptions')");
//$result1 = mysql_query("INSERT INTO users(userContactNumber) VALUES('$contact')");
// check if row inserted or not
if (($result)/*&& ($result1)*/) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Room added successfully.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
您的错误主要在于以下几行:
if(method == "POST"){
}else if(method == "GET")
java中的字符串比较必须使用equals
或equalsIgnoreCase
方法执行。在您的例子中,InputStream为空,并且从不初始化。因此,您试图将一个空字符串(StringBuilder.toString()
返回的字符串)转换为一个JSONObject
编辑:
改变
if(method == "POST"){
}else if(method == "GET")
与
然后看看它是否有什么不同(至少错误应该会改变)你的错误主要在以下几行:
if(method == "POST"){
}else if(method == "GET")
java中的字符串比较必须使用equals
或equalsIgnoreCase
方法执行。在您的例子中,InputStream为空,并且从不初始化。因此,您试图将一个空字符串(StringBuilder.toString()
返回的字符串)转换为一个JSONObject
编辑:
改变
if(method == "POST"){
}else if(method == "GET")
与
然后看看它是否有任何不同(至少错误应该改变)首先,按照“黑带”的建议,将其设置为if(method.equalsIgnoreCase(“post”)
。还有一个建议-
当从服务器接收到一些文本(无论是否是JSON)时,我更愿意在对响应执行任何操作之前检查它
来,试试看
HttpEntity httpEntity=httpResponse.getEntity();
String all=EntityUtils.toString(httpEntity);
Log.d("response",all);
首先,按照“黑带”的建议将其设置为if(method.equalsIgnoreCase(“post”))
。还有一个建议-
当从服务器接收到一些文本(无论是否是JSON)时,我更愿意在对响应执行任何操作之前检查它
来,试试看
HttpEntity httpEntity=httpResponse.getEntity();
String all=EntityUtils.toString(httpEntity);
Log.d("response",all);
Blackbelt和Nizam讨论了问题的Java方面。我将介绍PHP方面的内容
PHP脚本中最大的错误似乎如下所示(除非只是复制粘贴错误):
检查是否设置了POST参数“descriptions”。最后应该是“description”而不是“s”。Blackbelt和Nizam介绍了问题的Java方面。我将介绍PHP方面的内容
PHP脚本中最大的错误似乎如下所示(除非只是复制粘贴错误):
检查是否设置了POST参数“descriptions”。它应该是“description”,结尾不带“s”。您确定输入的是有效的JSON吗?在尝试创建JSON之前,请记录JSON字符串的内容
if (isset($_POST['quantity']) && isset($_POST['price']) && isset($_POST['descriptions'])) {