Android应用程序与服务器通信

时间:2014-01-16 07:10:52

标签: android android-asynctask

我是android的新手我必须将手机号码和pin发送到服务器,如果两者都正确则在服务器端然后我必须从该服务器接收成功数据,我不知道怎么请告诉我我试过这个但我现在不知道如何接收服务器数据

 my code 

public class MainActivity extends Activity implements OnClickListener{

private EditText value;
private Button btn;
private ProgressBar pb;
private TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    value=(EditText)findViewById(R.id.edit1);
    btn=(Button)findViewById(R.id.submitBtn);
    pb=(ProgressBar)findViewById(R.id.pb);
    tv =(TextView)findViewById(R.id.textView);

    pb.setVisibility(View.GONE);

    btn.setOnClickListener(this);

  }

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    if(value.getText().toString().length()<1){
        Toast.makeText(this, "please enter something", Toast.LENGTH_LONG).show();

    }else{
        pb.setVisibility(View.VISIBLE);
    }

}

 private class MyAsynTask extends AsyncTask<String, Integer, Double>{
  protected Double doInBackground(String... params) {
      // TODO Auto-generated method stub
      postData(params[0]);
      return null;
  }

  protected void onPostExecute(Double result){
      pb.setVisibility(View.GONE);
      Toast.makeText(getApplicationContext(), "Code Sent", Toast.LENGTH_LONG).show();
  }
  protected void onProgressUpdate(Integer... progress){
      pb.setProgress(progress[0]);
  }

  public void postData(String valueIWantToSend) {
      // Create a new HttpClient and Post Header
      HttpClient httpclient = new DefaultHttpClient();
      //HttpPost httppost = new HttpPost("http://10.0.2.2/chotu/index.php");
      HttpPost httppost = new HttpPost(" http://125.62.200.54/rest/login.php");
      try {

          // Add your data
          List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
          nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend));
          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

          // Execute HTTP Post Request
          HttpResponse response = httpclient.execute(httppost);

      } 
      catch (ClientProtocolException e) 
      {
          // TODO Auto-generated catch block
      } catch (IOException e)
      {
          // TODO Auto-generated catch block
      }
   }

1 个答案:

答案 0 :(得分:0)

BufferedReader bufferreader;
String line;

public void postData(String valueIWantToSend) {
      // Create a new HttpClient and Post Header
      HttpClient httpclient = new DefaultHttpClient();
      //HttpPost httppost = new HttpPost("http://"your domen"/chotu/index.php");
      HttpPost httppost = new HttpPost(" http://"your domen"/rest/login.php");
      try {

          // Add your data
          List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
          nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend));
          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

          // Execute HTTP Post Request
          HttpResponse response = httpclient.execute(httppost);

      } 
      catch (ClientProtocolException e) 
      {
          // TODO Auto-generated catch block
      } catch (IOException e)
      {
          // TODO Auto-generated catch block
      }
try{
    bufferreader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
       while ((line = bufferreader.readLine()) != null) {
        sb.append(line);
      }
    Log.v(tag, (String) String.valueOf(sb.toString()));
        if(sb.equals(null)){
        Log.v("Respone", sb.toString());
        return("notvalid");
    }
    bufferreader.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    Log.v("Respone", sb.toString());

   }