2012-06-19 82 views
0

我迄今完成的下面写的东西。我想要从使用JSON和打印数据库中的值.....但我被困在一定的误差,这是我甚至低于提交..等待您的帮助...Android的MySQL数据库连接

package com.yipl.Googlemaps; 

import android.app.Activity; 
import android.os.Bundle; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
//import java.util.ArrayList; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
//import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
//import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
//import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.util.Log; 




public class GoogleMapsActivity extends Activity 
{ 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     String result = ""; 
     InputStream is = null; 

     try{ 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("http://10.0.2.2/example/request.php"); 
     //  httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       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(); 
     }catch(Exception e){ 
       Log.e("log_tag", "Error converting result "+e.toString()); 
     } 

     //parse JSON data 
     try{ 
       JSONArray jArray = new JSONArray(result); 
       for(int i=0;i<jArray.length();i++){ 
         JSONObject json_data = jArray.getJSONObject(i); 
         Log.i("log_tag","id: "+json_data.getInt("id")+ 
           ", name: "+json_data.getString("name")+ 
           ", CHD: "+json_data.getString("CHD")+ 
           ", VDCs: "+json_data.getString("vdcs")+ 
           ", Children Vaccinated: "+json_data.getString("Vchildren")+ 
           ", Total: "+json_data.getString("total")+ 
           ", Latitude: "+json_data.getDouble("lat")+ 
           ", Longitude: "+json_data.getDouble("lng") 
         ); 
       } 

     }catch(JSONException e){ 
       Log.e("log_tag", "Error parsing data "+e.toString()); 
     } 


    } 
} 

服务代码.....

<?php 
    mysql_connect("localhost","root",""); 
    mysql_select_db("niporg"); 

    $q=mysql_query("select n.id, n.name, i.CHD, i.vdcs, i.Vchildren, i.total, l.lat, l.lng from info as i, nip_db as n, location as l where n.id=i.id and i.id = l.id"); 

    while($e=mysql_fetch_array($q)) 
    { 
     $output[]=$e; 

    } 

    print(json_encode($output)); 



    mysql_close(); 
    ?> 

但我得到错误..如下..

> 06-19 15:45:00.595: I/jdwp(267): received file descriptor 10 from ADB 
> 06-19 15:45:00.635: D/ddm-heap(267): Got feature list request 06-19 
> 15:45:00.815: D/AndroidRuntime(267): Shutting down VM 06-19 
> 15:45:00.815: W/dalvikvm(267): threadid=3: thread exiting with 
> uncaught exception (group=0x4001aa28) 06-19 15:45:00.815: 
> E/AndroidRuntime(267): Uncaught handler: thread main exiting due to 
> uncaught exception 06-19 15:45:00.835: E/AndroidRuntime(267): 
> java.lang.RuntimeException: Unable to start activity 
> ComponentInfo{com.yipl.Googlemaps/com.yipl.Googlemaps.GoogleMapsActivity}: 
> android.view.InflateException: Binary XML file line #7: Error 
> inflating class java.lang.reflect.Constructor 06-19 15:45:00.835: 
> E/AndroidRuntime(267): at 
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401) 
> 06-19 15:45:00.835: E/AndroidRuntime(267): at 
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417) 
> 06-19 15:45:00.835: E/AndroidRuntime(267): at 
> android.app.ActivityThread.access$2100(ActivityThread.java:116) 06-19 
> 15:45:00.835: E/AndroidRuntime(267): at 
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
> 06-19 15:45:00.835: E/AndroidRuntime(267): at 
> android.os.Handler.dispatchMessage(Handler.java:99) 06-19 
> 15:45:00.835: E/AndroidRuntime(267): at 
> android.os.Looper.loop(Looper.java:123) 06-19 15:45:00.835: 
> E/AndroidRuntime(267): at 
> android.app.ActivityThread.main(ActivityThread.java:4203) 06-19 
> 15:45:00.835: E/AndroidRuntime(267): at 
> java.lang.reflect.Method.invokeNative(Native Method) 06-19 
> 15:45:00.835: E/AndroidRuntime(267): at 
> java.lang.reflect.Method.invoke(Method.java:521) 06-19 15:45:00.835: 
> E/AndroidRuntime(267): at 
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
> 06-19 15:45:00.835: E/AndroidRuntime(267): at 
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) 06-19 
> 15:45:00.835: E/AndroidRuntime(267): at 
> dalvik.system.NativeStart.main(Native Method) 06-19 15:45:00.835: 
> E/AndroidRuntime(267): Caused by: android.view.InflateException: 
> Binary XML file line #7: Error inflating class 
> java.lang.reflect.Constructor 06-19 15:45:00.835: 
> E/AndroidRuntime(267): at 
> android.view.LayoutInflater.createView(LayoutInflater.java:512) 06-19 
> 15:45:00.835: E/AndroidRuntime(267): at 
> android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:564) 
> 06-19 15:45:00.835: E/AndroidRuntime(267): at 
> android.view.LayoutInflater.rInflate(LayoutInflater.java:617) 06-19 
> 15:45:00.835: E/AndroidRuntime(267): at 
> android.view.LayoutInflater.inflate(LayoutInflater.java:407) 06-19 
> 15:45:00.835: E/AndroidRuntime(267): at 
> android.view.LayoutInflater.inflate(LayoutInflater.java:320) 06-19 
> 15:45:00.835: E/AndroidRuntime(267): at 
> android.view.LayoutInflater.inflate(LayoutInflater.java:276) 06-19 
> 15:45:00.835: E/AndroidRuntime(267): at 
> com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:313) 
> 06-19 15:45:00.835: E/AndroidRuntime(267): at 
> android.app.Activity.setContentView(Activity.java:1620) 06-19 
> 15:45:00.835: E/AndroidRuntime(267): at 
> com.yipl.Googlemaps.GoogleMapsActivity.onCreate(GoogleMapsActivity.java:35) 
> 06-19 15:45:00.835: E/AndroidRuntime(267): at 
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 
> 06-19 15:45:00.835: E/AndroidRuntime(267): at 
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364) 
> 06-19 15:45:00.835: E/AndroidRuntime(267): ... 11 more 06-19 
> 15:45:00.835: E/AndroidRuntime(267): Caused by: 
> java.lang.reflect.InvocationTargetException 06-19 15:45:00.835: 
> E/AndroidRuntime(267): at 
> com.google.android.maps.MapView.<init>(MapView.java:237) 06-19 
> 15:45:00.835: E/AndroidRuntime(267): at 
> java.lang.reflect.Constructor.constructNative(Native Method) 06-19 
> 15:45:00.835: E/AndroidRuntime(267): at 
> java.lang.reflect.Constructor.newInstance(Constructor.java:446) 06-19 
> 15:45:00.835: E/AndroidRuntime(267): at 
> android.view.LayoutInflater.createView(LayoutInflater.java:499) 06-19 
> 15:45:00.835: E/AndroidRuntime(267): ... 21 more 06-19 15:45:00.835: 
> E/AndroidRuntime(267): Caused by: java.lang.IllegalArgumentException: 
> MapViews can only be created inside instances of MapActivity. 06-19 
> 15:45:00.835: E/AndroidRuntime(267): at 
> com.google.android.maps.MapView.<init>(MapView.java:281) 06-19 
> 15:45:00.835: E/AndroidRuntime(267): at 
> com.google.android.maps.MapView.<init>(MapView.java:254) 06-19 
> 15:45:00.835: E/AndroidRuntime(267): ... 25 more 06-19 15:45:00.855: 
> I/dalvikvm(267): threadid=7: reacting to signal 3 06-19 15:45:00.855: 
> E/dalvikvm(267): Unable to open stack trace file 
> '/data/anr/traces.txt': Permission denied 
+1

那么你最近的疑问是什么?想要存储数组中的值还是想从服务器返回JSON? –

+0

我想存储从查询阵列的价值......但是当我用Google搜索我知道,要做到这一点,我必须作出的HttpRequest和从服务器返回JSON格式.. – user98239820

回答

0

您需要为此使用JSON。下面是使用JSON

public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) { 

    // Making HTTP request 
    try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 
     httpPost.setEntity(new UrlEncodedFormEntity(params)); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     is = httpEntity.getContent(); 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    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(); 
     json = sb.toString(); 
     Log.e("JSON", json); 
    } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 

    // try parse the string to a JSON object 
    try { 
     jObj = new JSONObject(json); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

    // return JSON String 
    return jObj; 

} 

URL将是你会写你的查询,发回的结果Web服务文件的地址从MySQL数据库中检索值的代码。

+0

http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and -sqlite/ –

+0

检查此链接以获得有关android和mysql的清晰想法。 –

+0

谢谢你酷编译器为您的答复..因为我是新的JSON ..我无法弄清楚如何做到这一点..但我不能要求你做所有的编码我..你可以请给我在我的问题使用JSON的小提示.. 感谢您的帮助 – user98239820

0

第一u需要这样

<?php 
$con = mysql_connect("localhost","stool",""); 
if (!$con) 
{ 
die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db("stool", $con); 


$sql=mysql_query("select * from surveys"); 

while($row=mysql_fetch_assoc($sql)) $output[]=$row; 

print(json_encode($output)); 

mysql_close(); 

?> 

Web服务的文件,你可以进行更改,按您的要求。最后json_encode将所有的表信息发送到显示单元。那么你需要使用我首先提供的代码在你的android应用中调用这个服务文件。

+0

plz从给定链接下载代码并尝试理解它。 http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/ –

+0

::我已经编辑了问题..你可以建议我可能是什么错误.... – user98239820

0

我也是在我的应用程序中使用JSON。这里是我的代码,我如何检索数据库中的值

try{ 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(surveyURL); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     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); 
     sb = new StringBuilder(); 
     sb.append(reader.readLine() + "\n"); 

     String line="0"; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     result=sb.toString(); 
    }catch(Exception e){ 
     Log.e("log_tag", "Error converting result "+e.toString()); 
    } 
    try{ 
     jArray = new JSONArray(result); 
     JSONObject json_data=null; 
     json_data = jArray.getJSONObject(0); 
     quest_id=json_data.getString("question_id"); 
     question=json_data.getString("question"); 
     option1=json_data.getString("op"); 
     quesNo=json_data.getString("question_no"); 
     help=json_data.getString("Help"); 
     mandatory=json_data.getString("mandatory"); 
     others=json_data.getString("others"); 
     condition=json_data.getString("condition1"); 
     total++;  
    } 
    catch(JSONException e1) 
    { 
     Toast.makeText(getBaseContext(), "No Data Found" ,Toast.LENGTH_LONG).show(); 
    } catch (ParseException e1) 
    { 
     e1.printStackTrace(); 
    } 
+0

你把这些放在你的清单文件中: <使用权限android:name =“android.permission.ACCESS_NETWORK_STATE”<使用权限android:name =“android.permission.INTERNET” >