2013-11-27 248 views
0

我想获得主机名称的IP地址,然后在POST HTTP请求URL中使用它,我发送到 一切正常工作找到并添加getByName行后它突然崩溃 所以请帮助我,并告诉我究竟可能是什么问题? 预先感谢您getByName POST POST请求

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

行130:

变化return null;return false;

或者更聪明的东西是改变

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

return "1".equals(result);

+0

感谢我没有编辑,但是现在总是给我错误的用户名或密码 代码编辑 – user1928775

+0

您可以登录'result'所以我们可以帮助您? – Manitoba

+0

我添加了一个toast,它总是显示Host的值始终为空 – user1928775