【问题标题】:Json MySql get Two Integers and a StringJson MySql 得到两个整数和一个字符串
【发布时间】:2012-05-10 15:38:02
【问题描述】:

这是我获取 JSON 对象的类。在这段代码中,我只得到一个对象,我真的不知道如何从该方法返回,有一个 Protected Void 方法,它是一个调用的 settext 方法,并且是唯一的 JSON 对象所在的位置。

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 个参数的函数。任何人都可以帮助我或者可以给我一些提示吗? 谢谢!

【问题讨论】:

  • 您的 json 格式不正确。并且您没有使用循环来检索值
  • 你能给我一些建议吗?
  • 确定返回 json.getString(params[0]);这就是问题

标签: android mysql json object methods


【解决方案1】:

不要使用 AsyncTask,因为它在后台执行,这就是它给你带来一些问题的原因。

当你的 twitter arser 正常工作时,将它集成到 AsyncTask 中。以下代码未经测试。

 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;

    //}
}

}

【讨论】:

  • 我收到两个错误:new Read().execute("latitude"); String result = json.getString(params[i]);。它们都不能被重新定义为类型:params 和 Read。有什么建议吗?
  • 我应该如何处理这些参数?并删除新的读取执行?
猜你喜欢
  • 1970-01-01
  • 2022-09-28
  • 1970-01-01
  • 2020-11-29
  • 2013-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多