将android连接到webhost时出现Json错误
我为我的android应用程序创建了一个json类,将其连接到我的webhost数据库,下面是我的代码,在我的代码末尾我使用了try catch,但问题是在日志将android连接到webhost时出现Json错误,android,json,Android,Json,我为我的android应用程序创建了一个json类,将其连接到我的webhost数据库,下面是我的代码,在我的代码末尾我使用了try catch,但问题是在日志error parsing data org.json.JSONException:Valueï»中显示json解析器错误?无法将java.lang.String类型转换为JSONObject,有什么建议吗 public class JSONParser { static InputStream is = null; static JS
error parsing data org.json.JSONException:Valueï»中显示json解析器错误?无法将java.lang.String类型转换为JSONObject
,有什么建议吗
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(final String url) {
// Making HTTP request
try {
// Construct the client and the HTTP request.
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
// Execute the POST request and store the response locally.
HttpResponse httpResponse = httpClient.execute(httpPost);
// Extract data from the response.
HttpEntity httpEntity = httpResponse.getEntity();
// Open an inputStream with the data content.
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
// Create a BufferedReader to parse through the inputStream.
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
// Declare a string builder to help with the parsing.
StringBuilder sb = new StringBuilder();
// Declare a string to store the JSON object data in string form.
String line = null;
// Build the string until null.
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
// Close the input stream.
is.close();
// Convert the string builder data to an actual string.
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// Try to 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 the JSON Object.
return jObj;
}
// 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;
}
公共类JSONParser{
静态InputStream为空;
静态JSONObject jObj=null;
静态字符串json=“”;
//建造师
公共JSONParser(){
}
公共JSONObject getJSONFromUrl(最终字符串url){
//发出HTTP请求
试一试{
//构造客户端和HTTP请求。
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
//执行POST请求并在本地存储响应。
HttpResponse HttpResponse=httpClient.execute(httpPost);
//从响应中提取数据。
HttpEntity HttpEntity=httpResponse.getEntity();
//打开包含数据内容的inputStream。
is=httpEntity.getContent();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
//创建一个BufferedReader以通过inputStream进行解析。
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
is,“iso-8859-1”),8);
//声明字符串生成器以帮助解析。
StringBuilder sb=新的StringBuilder();
//声明一个字符串以字符串形式存储JSON对象数据。
字符串行=null;
//构建字符串直到为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;
}
//函数从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;
}
更换
json = sb.toString();
与
json = new String(sb.toString().getBytes("UTF-8"));
或
使用UTF-8而不是iso-8859-1确保在此处使用相同的字符编码:
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
正如服务器使用的一样。很可能不是ISO 8859-1。此外(尽管与问题无关),8字节的缓冲区大小低得离谱。您可以发布json(或至少发布部分json)吗?还要准确地发布logcat错误。这里是解析数据org.json.JSONException:Valueï»类型为java.lang.String的logcat错误
无法转换为JSONObject
检查是否仍然存在相同的问题,我的字符是英语而不是任何其他语言,对于登录,我使用字符串作为用户名,字符串作为密码,如:amir Arcane我如何在我的办公桌上打开本地主机url。请添加json输出我是json和android的新手,如何添加json输出?System.out.println(sb.toString())
我对andoid是新手,我刚刚复制并粘贴了这段web代码,我如何解决这个问题?我应该用什么来代替iso-8859-1?服务器在发送JSON时使用的是哪个字符编码?放一个,可能是UTF-8,但因为我不知道服务器…tnx,我用UTF-8更改了iso-8859-1,它可以工作吗?什么大约8个?你有什么建议吗?我会使用至少1024个。如果你的JSON通常比这个大,请使用more。好吧,解决这个问题,但是解析数据org.JSON.JSONException:Value时出现了一个新错误