嗨,我正在尝试在 android 中进行发送和接收。在哪里将一些参数发送到数据库并基于那些我想接收数据并在文本视图中显示但我找不到我的问题。请我解决这个代码。我会在后台做。
这是我的代码
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]);
}
});
}