1

请你能帮助我使用 android 库。

  1. 我点击了一个需要调用库的按钮。
  2. 图书馆依次启动登录屏幕
  3. 登录屏幕接受用户 ID,密码点击 URL 并返回响应(成功/失败)

http post(需要用作库)的代码是:

用户名 = (EditText) findViewById(R.id.editTextusername); 密码 = (EditText) findViewById(R.id.editTextpassword);

     submit = (Button)findViewById(R.id.submit);


     submit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

    // TODO Auto-generated method stub
    if(username.getText().length()>0 && password.getText().length()>0)
    {
        URLconnector ss = new URLconnector();
        ss.execute("http//ipchicken.com");
    }
}

});

}

private class URLconnector extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... urls) {
        String response = "";
        for (String url : urls) {

            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            try {



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



                UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
                        postParameters);

                httppost.setEntity(formEntity);

                HttpResponse response1 = httpclient.execute(httppost);

                BufferedReader in = new BufferedReader(
                        new InputStreamReader(response1.getEntity()
                                .getContent()));
                StringBuffer sb = new StringBuffer("");

                String line = "";
                String NL = System.getProperty("line.separator");
                while ((line = in.readLine()) != null) {
                    sb.append(line + NL);
                }
                in.close();

                String result = sb.toString();
                response = result;
                System.err.println("My Response :: " + result);

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

    }

    protected void onPostExecute(String result) {
        if(result != null)
        {
        Intent tokenIntent = new Intent(mContext, tokenActivity.class);
         Bundle bundle = new Bundle();
         bundle.putString("responsedata",result);

        tokenIntent.putExtras(bundle);
        startActivity(tokenIntent);
        }
4

0 回答 0