我被这个问题困住了。我正在尝试从 mysql 数据库中提取一些 json 数据。我目前想使用 AsyncTask 在后台完成大部分工作,并可能使用不同的 php 脚本来运行不同的查询返回不同的数据(这就是我没有将直接 url 插入到 httpost 的原因)
基本上现在,我想从数据库中检索数据,将其传递给一个变量,然后在 TextView 中显示该变量。
当我运行代码时,它只是在 TextView 中显示“无数据”,所以有些东西不起作用。我的 php 文件工作正常!继承人的代码:非常感谢任何帮助:)
package com.example.test1;
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.Activity;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class clubpage extends Activity {
TextView test;
String clubphp = "http://10.0.2.2/corkgaa/Nemo.php";
String event = "No Data";
String name = "No Data";
String price = "No Data";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.clubpage);
test = (TextView)findViewById(R.id.clubinfo);
new Dbhandler().execute(clubphp);
test.setText(name);
}
public class Dbhandler extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
JSONArray jArray;
String result = "no response";
InputStream is = null;
StringBuilder sb = null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("clubphp");
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());
}
//parsing data
try{
jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
name=json_data.getString("NAME");
event=json_data.getString("EVENT");
price=json_data.getString("PRICE");
}
}
catch(JSONException e1){
}
catch (ParseException e1) {
e1.printStackTrace();
}
return name;
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
日志猫:
03-04 15:25:35.139: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.149: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.239: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.259: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.259: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.270: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.279: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.279: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.312: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.312: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.349: I/ActivityManager(288): START u0 {cmp=com.example.test1/.clubpage} from pid 791
03-04 15:25:35.369: W/WindowManager(288): Failure taking screenshot for (246x410) to layer 21020
03-04 15:25:35.389: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.419: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.429: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.438: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.459: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.479: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.479: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.479: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.588: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.588: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.609: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.028: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.099: I/Choreographer(791): Skipped 31 frames! The application may be doing too much work on its main thread.
03-04 15:25:36.109: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.168: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.268: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.279: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.279: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.289: V/PhoneStatusBar(580): setLightsOn(true)
03-04 15:25:36.299: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.358: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.409: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
03-04 15:25:36.409: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.421: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.439: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.439: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.439: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.729: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.729: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.773: I/Choreographer(791): Skipped 158 frames! The application may be doing too much work on its main thread.
03-04 15:25:36.779: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.779: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.209: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.209: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.240: I/ActivityManager(288): Displayed com.example.test1/.clubpage: +1s832ms
03-04 15:25:37.269: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.269: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.299: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.309: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.331: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.399: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.589: I/Choreographer(791): Skipped 48 frames! The application may be doing too much work on its main thread.
03-04 15:25:37.649: D/dalvikvm(288): GC_CONCURRENT freed 980K, 35% free 7576K/11552K, paused 9ms+56ms, total 701ms
03-04 15:25:37.859: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.859: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.911: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.911: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.919: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.949: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.214: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.250: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.250: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.309: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.369: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.409: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.419: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.439: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.479: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.519: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.530: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.530: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.549: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.549: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.549: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.549: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.629: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.629: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.690: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.699: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.699: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.719: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.739: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.759: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0