Android外部在线数据库

时间:2014-03-10 11:49:39

标签: php android mysql

我在phpMyAdmin中有一个在线数据库。这时我可以查询他,我可以在TextView中得到结果,但我想得到每个单元格的值,我该怎么办?

这是一个例子

[{"Monday":"","Tuesday":"","Wednesday":"","Thursday":"","Friday":"","Saturday":"","Sunday":"","id":"","info":""},...]

我想以星期一的价值为例。

由于

代码sendQuery.java:

 public class sendQuery {
/////////// Public method to send Query ///////////
public static String send(String query) {
    String result = "0";
    InputStream is = null;
    String weekDayVal;
    //the query to send
    ArrayList<NameValuePair> querySend = new ArrayList<NameValuePair>();

    querySend.add(new BasicNameValuePair("querySend",query));

    //http post
    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://locali.altervista.org/php/locali.php");
        httppost.setEntity(new UrlEncodedFormEntity(querySend));
        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);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result=sb.toString();
        try{
            JSONArray weekDetails = new JSONArray ( result); // Your response string
            for(int index=0;index < weekDetails.length();index++)
            {
            JSONObject tempWeekDetail = weekDetails.getJSONObject(index);
            weekDayVal = tempWeekDetail.getString("Monday");// Value for Monday
            //added this Log which you can view from LogCat. also changed above variable name
            Log.i("Resp Value"," Moday Value"+weekDayVal);
            }
            }catch(Exception e)
            {
            //catch exception here
            }
    }catch(Exception e){
        Log.e("log_tag", "Error converting result: "+e.toString());
    }

    Log.i("SendQUERY", result);
    return result;
}

}

main.java:

 public class main extends Activity {
/** Called when the activity is first created. */
@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView resLayout = (TextView) findViewById(R.id.res);


    String res = sendQuery.send("SELECT * FROM contatti");

    resLayout.append(res);


}
}

2 个答案:

答案 0 :(得分:0)

尝试使用JSON,您可以轻松解析它并提取所需的数据

答案 1 :(得分:0)

您可以尝试以下代码。

/*String res = "[{\"Monday\":\"Work\",\"Tuesday\":\"\",\"Wednesday\":\"\",\"Thursday\":\"\",\"Friday\":\"\",\"Saturday\":\"\",\"Sunday\":\"\",\"id\":\"\",\"info\":\"\"}]";//you have to escape double quotes*/

try{
JSONArray weekDetails = new JSONArray ( result); // Your response string
for(int index=0;index < weekDetails.length();index++)
{
JSONObject tempWeekDetail = weekDetails.getJSONObject(index);
String weekDayVal = tempWeekDetail.getString("Monday");// Value for Monday
//added this Log which you can view from LogCat. also changed above variable name
Log.i("Resp Value"," Moday Value"+weekDayVal);
}
}catch(Exception e)
{
//catch exception here
}