2

新しい Android プログラムでは、1 つのリクエストを PHP ページに送信し、レスポンスを配列として受け取る必要があります。そのために、http 要求と応答機能を含むクラスを作成しました。リクエストを送信してレスポンスを受け取りますが、それらの配列を出力することはできず、それが配列または単一の文字列として送信されるかどうかはわかりません。誰でもこのバグを修正するのを手伝ってください。そして、ここに私のクラスがあります...

public class HistoryActivity extends Activity implements LocationListener{

    String p_u_name;
    HttpPost httppost;
    StringBuffer buffer;
    HttpResponse response;
    HttpClient httpclient;
    List<NameValuePair> nameValuePairs;
    ProgressDialog dialog = null;
    double park_latitude;
    double park_longitude;
    LocationManager locManager;
    HttpEntity entity;
    String rep;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history);

        SharedPreferences prefs = getSharedPreferences("myprefs", 0);

        p_u_name = prefs.getString("KEY_USERNAME", "");

              System.out.println("your name is"+p_u_name);

                locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, this);
                //get the current location (last known location) from the location manager
                Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                if(location!=null)
                {
                park_latitude = location.getLatitude();
                park_longitude = location.getLongitude();
                dialog = ProgressDialog.show(HistoryActivity.this, "", 
                        "Validating user...", true);
                 new Thread(new Runnable() {
                        public void run() {
                            parking();                          
                        }
                      }).start(); 

                }
                else
                {
                    Toast.makeText(HistoryActivity.this,"no location found", Toast.LENGTH_SHORT).show();    
                }

        }
    public void parking(){
         try{  
             httpclient=new DefaultHttpClient();
             httppost= new HttpPost("http://10.0.2.2/test_login/history.php");

             nameValuePairs = new ArrayList<NameValuePair>(2);

             nameValuePairs.add(new BasicNameValuePair("username",p_u_name.trim()));
             nameValuePairs.add(new BasicNameValuePair("park_latitude",Double.toString(park_latitude).trim()));
             nameValuePairs.add(new BasicNameValuePair("park_longitude",Double.toString(park_longitude).trim())); 
             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
             ResponseHandler<String> responseHandler = new BasicResponseHandler();


             String[] response =new String[2];

             for(int i=0; i<10; i++){
             response [i]= httpclient.execute(httppost, responseHandler);
             System.out.println("Response : " +response[i]); 
             runOnUiThread(new Runnable() {
                 public void run() {

                     dialog.dismiss();
                 }


             });
             }

int d=response.length;

System.out.println(d);


             if(d<1){
                 runOnUiThread(new Runnable() {
                     public void run() {
                         Toast.makeText(HistoryActivity.this,"Successfully parked", Toast.LENGTH_SHORT).show();

                     }
                 });

//               startActivity(new Intent(LoginActivity.this, MainActivity.class));
             }
             else{
                 Toast.makeText(HistoryActivity.this,"Message", Toast.LENGTH_SHORT).show();
                 showAlert();                
             }

         }catch(Exception e){
             dialog.dismiss();
             System.out.println("Exception : " + e.getMessage());
         }
     }
     public void showAlert(){
         HistoryActivity.this.runOnUiThread(new Runnable() {
             public void run() {
                 AlertDialog.Builder builder = new AlertDialog.Builder(HistoryActivity.this);
                 builder.setTitle("Parking Error.");
                 builder.setMessage("Try again?.")  
                        .setCancelable(false)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                Intent r = new Intent(getApplicationContext(),HistoryActivity.class);
                                startActivity(r);
                            }
                        });                     
                 AlertDialog alert = builder.create();
                 alert.show();               
             }
         });

     }
    public void onLocationChanged(Location arg0) {
        // TODO Auto-generated method stub

    }
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
}
4

1 に答える 1

2

ローカル ホストのアドレスを変更してみてください。他にエラーはありません。

于 2013-01-31T11:42:50.840 に答える