2011-12-21 34 views
0

いくつかの情報をTextViewに表示するには、MySQLデータベースに接続しようとしています。私はこのチュートリアルで試してみましたが、苦労しています。私のPHPはうまくいくはずです。私の心配はデータを扱うことです。私はJavaよりもC#に精通しているので、私の混乱が始まる場所である可能性があります。ここで MySQLデータベースに接続

は、私がこれまで持っているものです。

package shc_BalloonSat.namespace; 

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

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 

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.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
//import android.view.View; 
import org.json.JSONArray; 
import org.json.JSONException; 

public class Shc_BalloonSat_Activity extends Activity 
{ 
     int historyCountFromUser; 

    /** Called when the activity is first created. */ 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     assignInfoToInfoTextView(); 
     assignInfoToHistoryTextView(); 
    } 

    public void assignInfoToInfoTextView() 
    { 
     connect2DB(); 
     JSONArray jArray = new JSONArray(result); 

     TextView infoTV = (TextView)this.findViewById(R.id.info); 
     String infoText = "Last Known Altitude: " + /*put db info here*/ /*+*/ "\n"; 
     infoText += "Last Known Speed: " + /*put db info here*/ /*+*/ "\n"; 
     infoText += "Last Known Latitude:" + /*put db info here*/ /*+*/ "\n"; 
     infoText += "Last Known Longtitude: " + /*put db info here*/ /*+*/ "\n"; 

     infoTV.setText(infoText); 
    } 

    public void assignInfoToHistoryTextView() 
    {   
     connect2DB(); 
     JSONArray jArray = new JSONArray(result); 

     for (int count = 0; count <= 4; count++) 
     { 
       TextView historyTV = (TextView)this.findViewById(R.id.history); 
       String historyText = "Altitude: " + /*put db info here*/ /*+*/ "\n"; 
       historyText += "Speed: " + /*put db info here*/ /*+*/ "\n"; 
       historyText += "Latitude:" + /*put db info here*/ /*+*/ "\n"; 
       historyText += "Longtitude: " + /*put db info here*/ /*+*/ "\n\n"; 

       historyTV.append(historyText); 
     } 
    } 

    @SuppressWarnings("null") 

     public void connect2DB() 
    { 
     String result = ""; 
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 

     // Get data from database using HTTP Post 
     try 
     { 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("\* link to php file goes here */"); 
       httppost.setEntity(newUrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = new response.getEntity(); 
       InputStream is = entity.getContent(); 
     } 

     catch (Exception e) 
     { 
       Log.e("log_tag", "Error in HTTP connection "); 
     } 

     // Convert the data to a string that the phone can read 
     try 
     { 
       InputStream is = null; 
         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 "); 
     } 

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

     private HttpEntity newUrlEncodedFormEntity(
         ArrayList<NameValuePair> nameValuePairs) 
     { 
       // TODO Auto-generated method stub 
       return null; 
     }  
} 

私のクエリは、データベースから最後の5つの項目を引っ張ります。最後のものは通常の機能に、残りの4つは履歴機能に入れたいが、降順では最新のものから古いものへと編成されている。私はこれについて正しいことをしているように見えますか?

答えて

1

あなたはDBに接続しようとしていません。 Webサービスにデータを投稿しようとしています。結果が得られれば、クライアントコードは問題なく、接続に問題はありません。

実際の質問が結果を分割して別の場所に配置することに関連する場合は、結果がどのようなものかを教えてください。

関連する問題