正常情况下点击按钮会在下面文本显示数据库数据,但我调试时,点击按钮就立马闪退。
下面是我的客户端的代码:
public class MainActivity extends Activity {
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText tv = (EditText) findViewById(R.id.editView);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
// http get
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
"http://10.0.3.2/AndPHP.php");
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
int ct_id;
String ct_name;
try {
jArray = new JSONArray(result);
JSONObject json_data = null;
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
ct_id = json_data.getInt("id");
ct_name = json_data.getString("name");
tv.setText(ct_id);
tv.append(ct_name + " \n");
}
} catch (JSONException e1) {
Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
});
}
}
一下是Eclipse的日志:
05-04 05:26:40.845: W/ResourceType(1113): No package identifier when getting value for resource number 0x00000001
05-04 05:26:40.845: D/AndroidRuntime(1113): Shutting down VM
05-04 05:26:40.845: W/dalvikvm(1113): threadid=1: thread exiting with uncaught exception (group=0xa6194908)
05-04 05:26:40.845: E/AndroidRuntime(1113): FATAL EXCEPTION: main
05-04 05:26:40.845: E/AndroidRuntime(1113): android.content.res.Resources$NotFoundException: String resource ID #0x1
05-04 05:26:40.845: E/AndroidRuntime(1113): at android.content.res.Resources.getText(Resources.java:230)
05-04 05:26:40.845: E/AndroidRuntime(1113): at android.widget.TextView.setText(TextView.java:3769)
05-04 05:26:40.845: E/AndroidRuntime(1113): at com.example.test.MainActivity$1.onClick(MainActivity.java:142)
05-04 05:26:40.845: E/AndroidRuntime(1113): at android.view.View.performClick(View.java:4204)
05-04 05:26:40.845: E/AndroidRuntime(1113): at android.view.View$PerformClick.run(View.java:17355)
05-04 05:26:40.845: E/AndroidRuntime(1113): at android.os.Handler.handleCallback(Handler.java:725)
05-04 05:26:40.845: E/AndroidRuntime(1113): at android.os.Handler.dispatchMessage(Handler.java:92)
05-04 05:26:40.845: E/AndroidRuntime(1113): at android.os.Looper.loop(Looper.java:137)
05-04 05:26:40.845: E/AndroidRuntime(1113): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-04 05:26:40.845: E/AndroidRuntime(1113): at java.lang.reflect.Method.invokeNative(Native Method)
05-04 05:26:40.845: E/AndroidRuntime(1113): at java.lang.reflect.Method.invoke(Method.java:511)
05-04 05:26:40.845: E/AndroidRuntime(1113): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-04 05:26:40.845: E/AndroidRuntime(1113): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-04 05:26:40.845: E/AndroidRuntime(1113): at dalvik.system.NativeStart.main(Native Method)
------解决思路----------------------
jar包对了?result确定拿到了。。。。为什么会有问题呢。。。。奇怪
------解决思路----------------------
用的字符串资源没找到,android.content.res.Resources$NotFoundException