【发布时间】:2014-05-06 10:23:38
【问题描述】:
我正在尝试实现异步任务来满足 android 中的 json 功能。 json 函数在 android 2.3.3 中工作得很好。所以现在我需要将它实现到android中。 4.3 糖豆平台。但它无法像我预期的那样显示任何结果。异步任务功能有问题。请帮忙....
public class Update extends Activity{
JSONArray jArray = null;
String result = null;
StringBuilder sb = null;
InputStream is = null;
String ct_name;
LinearLayout l;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
Toast.makeText(getBaseContext(), "Done layout",Toast.LENGTH_LONG).show();
class LongOperation extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
//Why to use 10.0.2.2
HttpPost httppost = new HttpPost("http://10.0.2.2/moodle/myFile.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());
}
try{
jArray = new JSONArray(result);
JSONObject json_data=null;
l = (LinearLayout) findViewById(R.id.container);
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
ct_name=json_data.getString("name");//here "Name" is the column name in database
}
}
catch(JSONException e1){
Toast.makeText(getBaseContext(), "No Data Found" ,Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
return result;
}
protected void onPostExecute(String result) {
TextView txt = (TextView) findViewById(R.id.txt_1);
txt.setText("Executed");
Toast.makeText(getBaseContext(), "this is post",Toast.LENGTH_LONG).show();
Toast.makeText(getBaseContext(), ct_name,
Toast.LENGTH_LONG).show();
TextView textView = new TextView(null);
textView.setText(ct_name + " is out, please attempt it as soon as possible"+ "\n");
l.addView(textView);
}
}
Toast.makeText(getBaseContext(), "lol",
Toast.LENGTH_LONG).show();
}
}
/////////////////////////////////////// //////////////////////////////////// 可行的json代码
公共类 MainActivity 扩展 Activity {
JSONArray jArray = null;
String result = null;
StringBuilder sb = null;
InputStream is = null;
String ct_name;
LinearLayout l;
@Override
protected 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();
//Why to use 10.0.2.2
HttpPost httppost = new HttpPost("http://10.0.2.2/moodle/myFile.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());
}
try{
jArray = new JSONArray(result);
JSONObject json_data=null;
l = (LinearLayout) findViewById(R.id.container);
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
ct_name=json_data.getString("name");//here "Name" is the column name in database
Toast.makeText(getBaseContext(), ct_name,
Toast.LENGTH_LONG).show();
TextView textView = new TextView(this);
textView.setText(ct_name + " is out, please attempt it as soon as possible"+ "\n");
l.addView(textView);
}
}
catch(JSONException e1){
Toast.makeText(getBaseContext(), "No Data Found" ,Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
}
【问题讨论】:
-
在这里发布你的日志
-
检查我更新的答案...确保它会帮助你
标签: android json android-asynctask