Android Json解析错误字符串转换
我写了这段代码,然后ı写了httphandler等。但是当ı运行应用程序时,ı看到了错误:Android Json解析错误字符串转换,android,json,parsing,Android,Json,Parsing,我写了这段代码,然后ı写了httphandler等。但是当ı运行应用程序时,ı看到了错误: ............. Java解析错误:Java.lang.String类型的值无法转换为JSONObject ı在互联网上搜索,但ı无法解决。怎么了?ı应该在哪里添加代码 MainActivity.java 您需要接收JSON内容,但服务器正在返回HTML内容 修改服务器并将应用程序/json添加为内容类型: Content-type:application/json 这是服务器端错误,请检
.............
Java解析错误:Java.lang.String类型的值无法转换为JSONObjectı在互联网上搜索,但ı无法解决。怎么了?ı应该在哪里添加代码 MainActivity.java
您需要接收JSON内容,但服务器正在返回HTML内容 修改服务器并将
应用程序/json
添加为内容类型:
Content-type:application/json
这是服务器端错误,请检查您的响应。这是服务器端错误。您需要一个json字符串,但服务器返回html类型的响应。谢谢。解决了这个问题,只是忘记了字符串url=“htt p://api.androidhive.info/contacts/”中的“s”;应该是“https”
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
public class HttpHandler {
private static final String TAG = HttpHandler.class.getSimpleName();
public HttpHandler() {
}
public String makeServiceCall(String reqUrl) {
String response = null;
try {
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException: " + e.getMessage());
} catch (ProtocolException e) {
Log.e(TAG, "ProtocolException: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
return response;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
Content-type:application/json