2016-04-12 4 views
-1

私は2つのEditTextを持つインターフェイスでユーザーに要求するユーザー名とパスワードのパラメータでhttpリクエストを作成しようとしています。Http urlConnectionとAsyntaskでリクエストを取得する

私は次のようなエラーが発生しています: "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(); 
     } 
    } 

私はアプリケーションを実装するために自動生成をコード化していますインデックスAPI。

答えて

1

は、URL入力のparam

new MyTask().execute(httpPath); 
が欠落しています
関連する問題