0
import android.os.Bundle;

import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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 android.os.StrictMode;
import android.util.Log;
import android.widget.EditText;

public class login extends Activity {
Button button2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
EditText breakfast= (EditText)  findViewById(R.id.breakfast) ;
        EditText lunch = (EditText) findViewById(R.id.lunch);
        EditText dinner = (EditText) findViewById(R.id.dinner);
        EditText supper = (EditText) findViewById(R.id.supper);
        Button button = (Button) findViewById(R.id.button1) ;

        final String breakf = breakfast.getText().toString();
        final String lun = lunch.getText().toString();
        final String din = dinner.getText().toString();
        final String sup = supper.getText().toString();
button2=(Button)this.findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
   // generate your params:
   List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
   nameValuePairs.add(new BasicNameValuePair("breakfast", breakf));
   nameValuePairs.add(new BasicNameValuePair("Lunch", lun));
   nameValuePairs.add(new BasicNameValuePair("Dinner", din));
   nameValuePairs.add(new BasicNameValuePair("supper", sup));

   // send them on their way
   try {
       DefaultHttpClient httpClient = new DefaultHttpClient();

       HttpPost httpPost = new HttpPost("http://blahblah php?$username=caroline$password=scamper");

我有一个合适的网址,我只是把它删掉了

      // httpPost.setEntity(new UrlEncodedFormEntity(nameValueParams));

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

   } catch (UnsupportedEncodingException e) {
       e.printStackTrace();
   } catch (ClientProtocolException e) {
       e.printStackTrace();
   } catch (IOException e) {
       e.printStackTrace();
   }
}});}
//@Override
        public void onClick(View v) {
finish();
System.exit(0);
}
});
Intent intent = new Intent(Login.this, .class);
Login.this.startActivity(intent);
//new lister for save start db code their
//take info from button with sql




   @Override
   public void onClick(View arg0) {
       // TODO Auto-generated method stub
       postData();
});

   //
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} 

我知道我有一些错误,但我一直在改变它并且无法弄清楚为什么它不会改变任何帮助将非常感谢 php 应该可以工作我没有从应用程序中获得任何信息

4

1 回答 1

0

您不应该在主线程中调用网络连接。尝试使用 AsyncTask 以在后台线程上运行网络操作。

像这样试试

Class Login extends Activity
{
   protected void onCreate(Bundle savedInstanceState) 
   {
     button2.setOnClickListener(new OnClickListener() 
     {
       public void onClick(View arg0) 
       {
         Task t = new Task();
         t.execute();
       }
   }
   class Task extends AsyncTask<ArgType,ArgType,ArgType>
   {
     protected type doInBackground(ArgType... urls) 
     {
       try
       {
          //initiate httpconnection and get result
       }
       catch
       {
       }
     }
     protected void onPostExecute(ArgType result) 
     {
       //update some view or do some thing when background thread work is done
     }
   }
}

或者您可以参考http://www.android-ever.com/2012/10/android-asynctask-example.html了解更多关于 AsyncTask 的信息。

于 2013-04-08T21:37:32.723 回答