android通过传递某些参数从Mysql发送和接收数据
嗨,我正在尝试用android做发送和接收。其中发送一些参数到数据库,并基于这些参数,我希望接收数据并显示在文本视图中,但我找不到我的问题。请帮我解这个密码。我想在后台做些什么 这是我的密码android通过传递某些参数从Mysql发送和接收数据,android,Android,嗨,我正在尝试用android做发送和接收。其中发送一些参数到数据库,并基于这些参数,我希望接收数据并显示在文本视图中,但我找不到我的问题。请帮我解这个密码。我想在后台做些什么 这是我的密码 class Question extends AsyncTask<String, String, String> { /** * Before starting background thread Show Progress Dialog *
class Question extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(day1q1.this);
dialog.setMessage("Loading Question. Please wait...");
dialog.setIndeterminate(false);
dialog.setCancelable(false);
dialog.show();
}
/**
* getting All details from url
* */
protected String doInBackground(String... args)
{
String result = null;
InputStream is = null;
StringBuilder sb = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/get_Q.php");
nameValuePairs = new ArrayList<NameValuePair>();
// Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
nameValuePairs.add(new BasicNameValuePair("day",dayno.toString()));
nameValuePairs.add(new BasicNameValuePair("Qno",Q.toString()));
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());
}
JSONArray jArray;
try {
jArray = new JSONArray(result);
JSONObject json_data = null;
qns = new String[jArray.length()];
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
qns[i] = json_data.getString("Question");
}
} catch (JSONException e1) {
Toast.makeText(getBaseContext(), "Question", Toast.LENGTH_LONG)
.show();
} catch (ParseException e1) {
e1.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url)
{
dialog.dismiss();
runOnUiThread(new Runnable()
{
public void run()
{
System.out.println("usersMap.get(Question) : " + qns[0]);
Question.setText(qns[0]);
}
});
}
类问题扩展任务{
/**
*在启动后台线程显示进度对话框之前
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
dialog=新建进度dialog(第1天第一季度。此);
setMessage(“正在加载问题。请稍候…”);
dialog.setUndeterminate(false);
对话框。可设置可取消(false);
dialog.show();
}
/**
*从url获取所有详细信息
* */
受保护的字符串doInBackground(字符串…args)
{
字符串结果=null;
InputStream=null;
StringBuilder sb=null;
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://localhost/get_Q.php");
nameValuePairs=新的ArrayList();
//发布时始终使用相同的变量名,即android端变量名和php端变量名应相似,
添加(新的BasicNameValuePair(“day”,dayno.toString());
添加(新的BasicNameValuePair(“Qno”,Q.toString());
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
}
//将响应转换为字符串
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
is,“iso-8859-1”),8);
sb=新的StringBuilder();
sb.append(reader.readLine()+“\n”);
字符串行=“0”;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
杰索纳雷·贾雷;
试一试{
jArray=新的JSONArray(结果);
JSONObject json_data=null;
qns=新字符串[jArray.length()];
for(int i=0;i
是否存在错误?Stacktrace?或者它只是不起作用,并且可能不应该在问题中使用您的真实ip地址。您需要研究如何在GUI中更新线程或异步任务视图。您是否有相关代码?如果有,请发布。Darussian它不起作用。