Json MySql get Two Integers and a String

白昼怎懂夜的黑 提交于 2019-12-08 09:21:24

问题


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);
        } 
    }

}

}

What I want to do is to have an array where i could store three kind of objects (exemple Latitude[1], Longitude [1], Description[1]; Latitude[2] etc... I would like the latitude and longitude to be as integers ). After this I will use a for loop to call a function with this 3 parameters. Could anyone help me or could give me some tips ? Thank you!


回答1:


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

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;

    //}
}

}


来源:https://stackoverflow.com/questions/10537511/json-mysql-get-two-integers-and-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!