遇到json数据和android问题

时间:2013-03-04 01:14:47

标签: android json

我遇到了这个问题。我正在尝试从mysql数据库中提取一些json数据。我目前想使用AsyncTask在后台完成大部分工作,并可能使用不同的php脚本运行不同的查询返回不同的数据(这就是为什么我没有将直接url插入到httpost中)

现在基本上,我想从数据库中检索数据,将其传递给变量,然后在TextView中显示该变量。

当我运行代码时,它只在TextView中显示“No Data”,因此无法正常工作。我的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();
}

}

Log cat:

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

3 个答案:

答案 0 :(得分:0)

您需要在onPostExecute()类中添加Dbhandler方法,并在该方法中设置TextView的文本值:

public class Dbhandler extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... arg0) {
        [...]
        return name;
    }

    @Override
    protected void onPostExecute(String newName) {
        test.setText(newName);
    }
}

答案 1 :(得分:0)

HttpPost httppost = new HttpPost(“clubphp”);

只需使用以下代码替换此行

HttpPost httppost = new HttpPost(arg0 [0]);

在post执行时实现如下

@Override
    protected void onPostExecute(String resultName) {
       test.setText(resultName);
    }
希望它能奏效 如果您发现任何问题,请在此处发布日志猫

答案 2 :(得分:0)

尝试将您的10.0.2.2替换为您的系统IP地址,并在返回名称之前添加一个打印名称的日志。在doInbackground中,知道系统的ip地址,在命令提示符下执行ipconfig