2016-04-27 64 views
0

我有应该发送的电话号码,并从数据库中检索的值的应用程序显示在数据库中的多个列中的值,现在我改变了查询,我的代码应检索多个列的值,那么,改变应该在我的代码中。通过Android的

public class JSONTransmitter extends AsyncTask<JSONObject, Void, String> 
{ 
    HttpResponse response; 

    String url = "http://192.168.1.97:89/Derdeery/bankOsman.php"; 

    private AsyncCallback asyncCallback; 

    public JSONTransmitter(Context context) { 
     // attach the callback to any context 
     asyncCallback = (AsyncCallback) context; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
     asyncCallback.onResponse(result); 
    } 

    protected String doInBackground(JSONObject... data) 
    { 
     JSONObject json = data[0]; 

     HttpClient client = new DefaultHttpClient(); 
     HttpConnectionParams.setConnectionTimeout(client.getParams(), 100000); 
     StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build()); 
     JSONObject jsonResponse = null; 

     HttpPost post = new HttpPost(url); 


     String resFromServer = ""; 

     try { 
      StringEntity se = new StringEntity("json=" + json.toString()); 

      post.addHeader("content-type", "application/x-www-form-urlencoded"); 

      post.setEntity(se); 

      HttpResponse response; 
      response = client.execute(post); 
      resFromServer = org.apache.http.util.EntityUtils.toString(response.getEntity()); 

      Log.i("Response from server", resFromServer); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return resFromServer; 
    } 

    public static interface AsyncCallback { 
     void onResponse(String res); 
    } 
} 

回答

0

这是值得考虑的:

  • 首先,在你的PHP代码(服务器端),也许单独查询数据。因此,举例来说,你可以得到那些符合列A中,然后(相应的A和B)查询那些符合B列
  • 是生成JSON每个
  • 然后创建一个包含两套一个JSON对象

    :像这样的数据{ “DataFromColumnA”:{}, “DataFromColumnB”:{} }

  • 一旦你已经在你的Android代码所做的HTTP请求,你可以得到具体的数据通过分别获得“DataFromColumnA”json Object和B.

我希望这可以帮助您解决问题!

+0

对不起,我改变了我的问题如果有未知数量的列来检索它们的值,该怎么办 – JEREEF

+0

我认为最好的方法是将您的请求与需要在数据库中查询的任何列名称一起发送 - 这样,您可以使用它们轻松映射远程服务器中的列 – Eenvincible