我使用JSON在android中为客户端服务器通信做了一个代码。我正在使用WAMP服务器,PHP文件中没有错误。当我运行我的应用程序模拟器显示“不幸的是,myapp已停止”。请建议我的想法。我试图修复,但我不能。 我的java文件:
package com.example.http;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb=null;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/android_connect/read1.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//paring data
try{
jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
String ct_name = json_data.getString("User_ID");
tv = (TextView) findViewById(R.id.textView1);
tv.setText(ct_name);
}
}
catch(JSONException e1){
Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
}
和PHP文件:
<?php
mysql_connect("localhost","root","");
mysql_select_db("androidhive");
$sql=mysql_query("select User_ID from opass");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>
logcat错误:
04-21 22:43:33.502: E/AndroidRuntime(399): FATAL EXCEPTION: main
04-21 22:43:33.502: E/AndroidRuntime(399): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.http/com.example.http.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-21 22:43:33.502: E/AndroidRuntime(399): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-21 22:43:33.502: E/AndroidRuntime(399): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-21 22:43:33.502: E/AndroidRuntime(399): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-21 22:43:33.502: E/AndroidRuntime(399): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-21 22:43:33.502: E/AndroidRuntime(399): at android.os.Handler.dispatchMessage(Handler.java:99)
04-21 22:43:33.502: E/AndroidRuntime(399): at android.os.Looper.loop(Looper.java:137)
04-21 22:43:33.502: E/AndroidRuntime(399): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-21 22:43:33.502: E/AndroidRuntime(399): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 22:43:33.502: E/AndroidRuntime(399): at java.lang.reflect.Method.invoke(Method.java:511)
04-21 22:43:33.502: E/AndroidRuntime(399): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-21 22:43:33.502: E/AndroidRuntime(399): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-21 22:43:33.502: E/AndroidRuntime(399): at dalvik.system.NativeStart.main(Native Method)
04-21 22:43:33.502: E/AndroidRuntime(399): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-21 22:43:33.502: E/AndroidRuntime(399): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
04-21 22:43:33.502: E/AndroidRuntime(399): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273)
04-21 22:43:33.502: E/AndroidRuntime(399): at android.app.Activity.setContentView(Activity.java:1881)
04-21 22:43:33.502: E/AndroidRuntime(399): at com.example.http.MainActivity.onCreate(MainActivity.java:40)
04-21 22:43:33.502: E/AndroidRuntime(399): at android.app.Activity.performCreate(Activity.java:5104)
04-21 22:43:33.502: E/AndroidRuntime(399): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-21 22:43:33.502: E/AndroidRuntime(399): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
答案 0 :(得分:2)
扩展ListActivity
并提供自定义布局时,您必须将以下 ID 设置为xml中的ListView
- 在本例中为{{1} }。
activity_main
顺便说一句,例外本身就足够说明了。
答案 1 :(得分:1)
您的内容必须为
ListView
,其id
属性为'android.R.id.list'
你的R.layout.activity_main没有listview元素。
PS:您不应该在主线程上使用关于AsyncTask
的网络请求答案 2 :(得分:0)
从错误消息:
04-21 22:43:33.502: E/AndroidRuntime(399): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.http/com.example.http.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
您的“activity_main.xml”可能没有id为“android.R.id.list”的属性。 由于您的活动扩展了“ListActivity”,因此您应该包含它。