JSON的MySql获得两个整数和一个字符串整数、字符串、两个、JSON

2023-09-07 10:49:34 作者:らforget゜旧事

下面就是我得到的JSON对象的类。在这个code我只得到一个对象,我真的不知道如何从方法返回,有一个保护无效的方法,其中它被称为setText方法并有其中唯一的JSON对象去。

Here is the class where i get JSON objects. In this code I get only one object and I don't really know how to return from the method, there is a Protected Void method where it is a settext method called and there is where the only JSON object goes.

public class ConnectMySql extends Activity {

 TextView httpStuff;
 HttpClient client;
 JSONObject json;

 final static String URL = "http://79.114.48.119/RadarsMySql.php";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    httpStuff = (TextView) findViewById(R.id.tvHttp);
    client = new DefaultHttpClient();
    new Read().execute("latitude");
}


public JSONObject lastTweet(String username) throws ClientProtocolException, IOException,JSONException{
    StringBuilder url = new StringBuilder(URL);
    url.append(username);


    HttpGet get = new HttpGet(url.toString());
    HttpResponse r = client.execute(get);
    int status = r.getStatusLine().getStatusCode();
    //if(status == 200){
        HttpEntity e = r.getEntity();

        String data = EntityUtils.toString(e);
        data = data.substring(data.indexOf("["));

        JSONArray timeline = new JSONArray(data);
        JSONObject last = timeline.getJSONObject(0);
        return last;

    //}else{ 
        //Toast.makeText(ConnectMySql.this, "error", Toast.LENGTH_LONG);
        //return null;

    //}
}

public class Read extends AsyncTask<String, Integer, String>{

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        try {
            json = lastTweet("");
            return json.getString(params[0]);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void onPostExecute(String result) {
        // TODO Auto-generated method stub
        httpStuff.setText(result);
        int myNum = 0;

        try {
            myNum = Integer.parseInt(result);
            httpStuff.setText(myNum);
        } catch(NumberFormatException nfe) {
           System.out.println("Could not parse " + nfe);
        } 
    }

}

}

我想要做的是有一个数组在那里我可以存储三种对象(纬度为例[1],经度[1],说明[1];纬度[2]等等。我想的纬度和经度是作为整数)。在此之后,我会用一个for循环调用函数与此3个参数。谁能帮我还是可以给我一些建议吗?谢谢!

推荐答案

不使用AsyncTask的,因为它在后台执行,这就是为什么它会导致一些问题给你。

Dont use AsyncTask, as it executes in background, and this is why it causes some problems for you.

当你有你的Twitter arser的工作,在一个AsyncTask的它集成。低于code未经测试。

And when you have your twitter arser working, integrate it in a AsyncTask. Code below is not tested.

 public class ConnectMySql extends Activity {

 TextView httpStuff;
 HttpClient client;
 int i;
 JSONObject json;

 final static String URL = "http://localhost/RadarsMySql.php";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    httpStuff = (TextView) findViewById(R.id.tvHttp);
    client = new DefaultHttpClient();
    for(i=0;i<2;i++){
    new Read().execute("latitude");

        try {
            json = lastTweet("",i);

            String result = json.getString(params[i]);

         httpStuff.setText(result);
            int myNum = 0;

            try {
                myNum = Integer.parseInt(result);
                httpStuff.setText(myNum);
            } catch(NumberFormatException nfe) {
               System.out.println("Could not parse " + nfe);
            } 


        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}


public JSONObject lastTweet(String username,int i) throws ClientProtocolException, IOException,JSONException{
    StringBuilder url = new StringBuilder(URL);
    url.append(username);


    HttpGet get = new HttpGet(url.toString());
    HttpResponse r = client.execute(get);
    int status = r.getStatusLine().getStatusCode();
    //if(status == 200){
        HttpEntity e = r.getEntity();

        String data = EntityUtils.toString(e);
        data = data.substring(data.indexOf("["));

        JSONArray timeline = new JSONArray(data);
        JSONObject last = timeline.getJSONObject(i);
        return last;

    //}else{ 
        //Toast.makeText(ConnectMySql.this, "error", Toast.LENGTH_LONG);
        //return null;

    //}
}

}