POST HTTP请求中的getByName

时间:2013-11-27 14:33:57

标签: android http url post android-asynctask

我试图获取主机名的IP地址然后在POST Http请求URL中使用它我发送到 一切都工作找到,并在添加getByName行后突然崩溃 那么请帮帮我,告诉我究竟是什么问题? 提前谢谢

Logindb.java

    package com.example.loginad;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;

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.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Logindb extends Activity {
Button login;
EditText u,p;
TextView res;
String result;
String x="mobile";
String host="";
Thread mBack;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.logindb);

        login=(Button)findViewById(R.id.login);
        u=(EditText)findViewById(R.id.u);
        p=(EditText)findViewById(R.id.p);
        res=(TextView)findViewById(R.id.res);

        mBack = new Thread(new Runnable()
        {

        public void run()
        {
            try
              {
                     InetAddress address=null;
               address = InetAddress.getByName("Osama-PC");
                    host=address.getHostAddress();
              }
              catch(Exception e)
              {
                 e.printStackTrace();
              }         


        }
    }); 
        login.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                new MyAsyncTask().execute(u.getText().toString(),p.getText().toString());


            }
        });



    }
    private class MyAsyncTask extends AsyncTask<String, Integer, Boolean>{

        @Override
        protected Boolean doInBackground(String... params) {
            // TODO Auto-generated method stub
            boolean success = postData(params[0],params[1]);        
            return success;
        } 

        protected void onPostExecute(Boolean localres){

             if (localres){
                    res.setText("A Correct Username and Password");
                }else{
                    res.setText("Incorrect Username or Password");
                }
                Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_LONG).show();
        }
        protected void onProgressUpdate(Integer... progress){
            //pb.setProgress(progress[0]);
            //Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_LONG).show();

        }


        public Boolean postData(String a,String b) {
              ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
              postParameters.add(new BasicNameValuePair("username", a));
              postParameters.add(new BasicNameValuePair("password", b));
              postParameters.add(new BasicNameValuePair("mobileid",x));
              // String valid = "1";
              String response = null;
              try {                 


                //  Toast.makeText(getApplicationContext(), host.toString(), Toast.LENGTH_LONG).show();
                 response = CustomHttpClient.executeHttpPost("http://"+host+"/new/check.php",postParameters);
//now in result you will have the response from php file either 0 or 1.                        
   result = response.toString();
                  // res = res.trim();
                  result = result.replaceAll("\\s+", "");
                  // error.setText(res);

              } catch (Exception e) {
                  //res.setText(e.toString());
              }

           return "1".equals(result);





        }



    }





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

}

1 个答案:

答案 0 :(得分:1)

第130行:

return null;更改为return false;

或者更聪明的事情就是改变

if(result!=null)
{
    return result.equals("1");
}
else
{
    return null;
}

return "1".equals(result);