0

我想创建一个电子邮件登录页面,以访问我的网络邮件,而我在输入 ID 和密码后尝试连接到邮件,它显示了我的网络邮件登录的 html 代码,而不是将我带入我的收件箱。这是我的代码。\

主要活动:-

公共类 AndroidLogin 扩展 Activity 实现 OnClickListener {

按钮确定,返回,退出;文本视图结果;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Login button clicked
    ok = (Button)findViewById(R.id.btn_login);
    ok.setOnClickListener(this);

    result = (TextView)findViewById(R.id.lbl_result);

}

public void postLoginData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();

    /* login.php returns true if username and password is equal to saranga */
    HttpPost httppost = new HttpPost("http://www.gowdanar.com:2095/");

    try {
        // Add user name and password
     EditText uname = (EditText)findViewById(R.id.txt_username);
     String username = uname.getText().toString();

     EditText pword = (EditText)findViewById(R.id.txt_password);
     String password = pword.getText().toString();

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("username", username));
        nameValuePairs.add(new BasicNameValuePair("password", password));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        Log.w("SENCIDE", "Execute HTTP Post Request");
        HttpResponse response = httpclient.execute(httppost);

        String str = inputStreamToString(response.getEntity().getContent()).toString();
        Log.w("SENCIDE", str);

        if(str.toString().equalsIgnoreCase("true"))
        {
         Log.w("SENCIDE", "TRUE");
         result.setText("Login successful");  
        }else
        {
         Log.w("SENCIDE", "FALSE");
         result.setText(str);            
        }

    } catch (ClientProtocolException e) {
     e.printStackTrace();
    } catch (IOException e) {
     e.printStackTrace();
    }
}

private StringBuilder inputStreamToString(InputStream is) {
 String line = "";
 StringBuilder total = new StringBuilder();
 // Wrap a BufferedReader around the InputStream
 BufferedReader rd = new BufferedReader(new InputStreamReader(is));
 // Read response until the end
 try {
  while ((line = rd.readLine()) != null) {
    total.append(line);
  }
 } catch (IOException e) {
  e.printStackTrace();
 }
 // Return full string
 return total;
}

@Override
public void onClick(View view) {
  if(view == ok){
    postLoginData();
  }
}

}

4

1 回答 1

1

将你的 http 请求放在 AsynTask 中,然后在 onPostExecute() 方法中得到响应。

私有类 GetResult 扩展 AsyncTask {

    @Override
    protected String doInBackground(String... urls) {
        Log.v(TAG + ".doInBackground", "doInBackground method call");
        String response1 = null;

    /*  for (String url : urls) {
            WebHelper webHelper = new WebHelper();
            response = webHelper.getResult(url);
            Log.v(TAG+".doInBackground", "json response is:" + response);
        }*/

           HttpClient httpclient = new DefaultHttpClient();
           HttpPost  httppost = new HttpPost(""http://www.gowdanar.com:2095/"");
          //  Log.d("response", "WORKING");
            try {


                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

                nameValuePairs.add(new BasicNameValuePair("username", username));
                nameValuePairs.add(new BasicNameValuePair("password", password));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                InputStream is = response.getEntity().getContent();
                WebHelper webHelper = new WebHelper();
                response1 = webHelper.convertStreamToString(is);
                Log.v(TAG+".doInBackground", "json response is:" + response1);

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


        return response1;


    }

    @Override
    protected void onPostExecute(String result) {

        Log.v(TAG + ".onPostExecute", "onPostExecute method call");
        dialog.dismiss();
        Log.v(TAG+".onPostExecute", "json response is:" + result);


         if(result!=null){

            try {
                //JSONTokener tokener = new JSONTokener(result);
                JSONObject resultObjct = new JSONObject(result);
                String user_id=resultObjct.getString("User_ID");


                }

                } catch (JSONException e) {
                e.printStackTrace();
                }
               catch (Exception e) {
                e.printStackTrace();
               }
             }

    }
}
于 2013-05-08T05:02:59.273 回答