使用urlConnection和Asyntask获取请求

时间:2016-04-12 15:33:52

标签: android android-asynctask httpurlconnection http-get

我正在尝试使用带有两个EditText的界面向用户请求的用户名和密码参数发出http请求。

我遇到以下错误:“执行doInBackground时发生错误()”感谢您的帮助。

这是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.leonelbaidal.moodlenots">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
     App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

</manifest>

主要活动:

public class MainActivity extends Activity {

    HttpURLConnection urlConnection = null;
    InputStream is;
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        final EditText password;
        final EditText username;
        Button login;

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        username = (EditText) findViewById(R.id.txt_username);
        password = (EditText) findViewById(R.id.txt_password);
        login = (Button) findViewById(R.id.btn_loggin);

        password.setTypeface(Typeface.DEFAULT);
        password.setTransformationMethod(new PasswordTransformationMethod());

        final String httpPath = "http://10.0.23.34/android/login.php?u=" + username.getText().toString() + "&p=" + password.getText().toString();

        login.setOnClickListener(new View.OnClickListener()  {
            @Override

            public void onClick(View v){
                new MyTask().execute();
            }
        });

        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

任务类:

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

        String textResult;

        @Override
        protected Void doInBackground(String... httpPath){


            try {
                URL url = new URL(httpPath[0]);
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setDoOutput(true);

                BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                String stringBuffer;
                String stringText = "";
                while ((stringBuffer = in.readLine()) != null) {
                    stringText += stringBuffer;
                }

                in.close();
                textResult = stringText;

            } catch (MalformedURLException e) {
                e.printStackTrace();
                textResult = e.toString();
            } catch (IOException e){
                e.printStackTrace();
                textResult = e.toString();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result){
            Toast.makeText(getApplicationContext(), textResult, Toast.LENGTH_SHORT).show();
        }
    }

最后,我使用自动生成编码来实现App Indexing API。

1 个答案:

答案 0 :(得分:1)

您缺少网址输入参数

new MyTask().execute(httpPath);