我用Asp.net MVC创建一个API,它返回JSON。现在我想在我的Android应用程序中接收JSON,但它有错误。如何获取Json在android
我返回[{"id":22,"Name":"2","Family":"1"}]
,我的浏览器显示在http://192.168.1.100/api/people。
这里是我的Android代码:
public class ReteriveFormWebActivity extends Activity {
private static String url = "http://192.168.1.100/api/people";
//JSON Node Names
JSONArray user = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost(url);
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
Log.i("re", result);
}
catch (Exception e) {
// Oops
} finally {
try {
if (inputStream != null)
inputStream.close();
}
catch (Exception squish) {}
}
}
}
在日志中显示错误eccurred:为什么呢?
'在日志中显示错误eccurred'哪个错误?显示应用程序崩溃时的logcat结果 –
请有人帮我 – aliyousefian
在日志中显示错误eccurred哪个错误?显示应用程序崩溃时的logcat结果 –