0

我正在开发一个简单的客户端服务器登录应用程序。该应用程序在模拟器上运行良好,但是当我从手机运行该应用程序时它不起作用。我还添加了<uses-permission android:name="android.permission.INTERNET"/> 通过 wifi 连接我的 android 手机的权限,并通过 ping 我的服务器测试了与终端模拟器应用程序的连接性。

URL = "http://192.168.1.2:8084/AndroidServlet/AndroidServlet?name="+name+"&pass="+pass;

这是我用来连接服务器的 URL,192.168.1.2 是我机器的 IP 地址。

public class SimServletActivity extends Activity implements OnClickListener{
Button button;
String name,pass;
EditText us_id,us_password;
public String URL = "http://192.168.1.1:8084/AndroidServlet/AndroidServlet?name="+name+"&pass="+pass;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    findViewsById();
    button.setOnClickListener(this);
}

private void findViewsById() {
    button = (Button) findViewById(R.id.btnLogin);
    us_id=(EditText) findViewById(R.id.loginid);
    us_password=(EditText) findViewById(R.id.password);
}
public void getDetails(){
     name=us_id.getText().toString();
     pass=us_password.getText().toString();
     URL = "http://192.168.1.1:8084/AndroidServlet/AndroidServlet?name="+name+"&pass="+pass;
}

public void onClick(View view) {
    getDetails();
    GetXMLTask task = new GetXMLTask();
    task.execute(new String[] { URL });
}

private class GetXMLTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        String output = null;
        for (String url : urls) {
            output = getOutputFromUrl(url);
        }
        return output;
    }

    private String getOutputFromUrl(String url) {
        String output = null;
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            output = EntityUtils.toString(httpEntity);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return output;
    }

    @Override
    protected void onPostExecute(String output) {

        if(output.equals("true"))
        {
            Intent i=new Intent(SimServletActivity.this, NextActivity.class);
            startActivity(i);
        }
        else{
            Toast.makeText(SimServletActivity.this, "Enter correct Details",Toast.LENGTH_LONG).show();
        }


    }
}

}

任何帮助表示赞赏。提前致谢。

LogCat 详细信息是E/AndroidRuntime(26414): FATAL EXCEPTION: main E/AndroidRuntime(26414): java.lang.NullPointerException E/AndroidRuntime(26414): at com.eg.simpleservlet.SimServletActivity$GetXMLTask.onPostExecute(SimServletActivity.java:88) E/AndroidRuntime(26414): at com.eg.simpleservlet.SimServletActivity$GetXMLTask.onPostExecute(SimServletActivity.java:1) E/AndroidRuntime(26414): at android.os.AsyncTask.finish(AsyncTask.java:417) E/AndroidRuntime(26414): at android.os.AsyncTask.access$300(AsyncTask.java:127) E/AndroidRuntime(26414): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429) E/AndroidRuntime(26414): at android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime(26414): at android.os.Looper.loop(Looper.java:130) E/AndroidRuntime(26414): at android.app.ActivityThread.main(ActivityThread.java:3687) E/AndroidRuntime(26414): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime(26414): at java.lang.reflect.Method.invoke(Method.java:507) E/AndroidRuntime(26414): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) E/AndroidRuntime(26414): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) E/AndroidRuntime(26414): at dalvik.system.NativeStart.main(Native Method)

4

1 回答 1

0

如果您的网站返回空字符串,

if(output.equals("true"))

会像你描述的那样失败。你可以使用

if ((output!=null) && output.equals("true"))

反而。

于 2013-04-01T18:21:30.443 回答