2015-11-23 13 views
0

我是Android的新品。試圖做一個簡單的GET請求。 這是我的代碼。主要是直接從[在這裏輸入鏈接描述] 我做錯了什麼?在Android中使用HttpURLConnection會產生403錯誤

的AndroidManifest.xml

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

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

    <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:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

MainActivity.java

 package com.beckysolomon.ravelry; 

     import android.app.Activity; 
     import android.os.Bundle; 
     import android.view.Menu; 
     import android.view.MenuItem; 


     public class MainActivity extends Activity { 



     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      //TextView result = (TextView)findViewById(R.id.results); 
      new RequestActivity(this).execute("www.google.com"); 

     } 


     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      // Inflate the menu; this adds items to the action bar if it is present. 
      getMenuInflater().inflate(R.menu.menu_main, menu); 
      return true; 
     } 

     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      // Handle action bar item clicks here. The action bar will 
      // automatically handle clicks on the Home/Up button, so long 
      // as you specify a parent activity in AndroidManifest.xml. 
      int id = item.getItemId(); 

      //noinspection SimplifiableIfStatement 
      if (id == R.id.action_settings) { 
       return true; 
      } 

      return super.onOptionsItemSelected(item); 
     } 

    } 

RequestActivity.java

package com.beckysolomon.ravelry; 

    import android.app.Activity; 
    import android.os.AsyncTask; 
    import android.util.Log; 
    import android.widget.TextView; 

    import java.io.IOException; 
    import java.io.InputStream; 
    import java.io.InputStreamReader; 
    import java.io.Reader; 
    import java.net.HttpURLConnection; 
    import java.net.URL; 

    /** 
    * Created by beckysolomon on 11/22/15. 
    * 
    * Help from : 
    * http://stackoverflow.com/questions/3505930/make-an-http-request-with-android 
    * http://developer.android.com/training/basics/network-ops/connecting.html 
    */ 
    class RequestActivity extends AsyncTask<String, String, String> { 

     Activity activity; 
     String DEBUG_TAG = "becky"; 

     public RequestActivity(Activity activity) { 
      this.activity = activity; 
     } 

     @Override 
     protected String doInBackground(String... urls) { 
      // params comes from the execute() call: params[0] is the url. 
      try { 
       Log.d(DEBUG_TAG, urls[0]); 
       return downloadUrl(urls[0]); 
      } catch (IOException e) { 
       return "Unable to retrieve web page. URL may be invalid."; 
      } 
     } 

     // Given a URL, establishes an HttpUrlConnection and retrieves 
    // the web page content as a InputStream, which it returns as 
    // a string. 
     private String downloadUrl(String myurl) throws IOException { 
      InputStream is = null; 
      // Only display the first 500 characters of the retrieved 
      // web page content. 
      int len = 500; 

      try { 
       URL url = new URL(myurl); 
       Log.d(DEBUG_TAG, myurl); 
       HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
       conn.setReadTimeout(10000 /* milliseconds */); 
       conn.setConnectTimeout(15000 /* milliseconds */); 
       conn.setRequestMethod("GET"); 
       conn.setDoInput(true); 
       // Starts the query 
       conn.connect(); 
       int response = conn.getResponseCode(); 
       Log.d(DEBUG_TAG, "The response is: " + response); 
       is = conn.getInputStream(); 

       // Convert the InputStream into a string 
       String contentAsString = readIt(is, len); 
       return contentAsString; 

       // Makes sure that the InputStream is closed after the app is 
       // finished using it. 
      } finally { 
       if (is != null) { 
        is.close(); 
       } 
      } 
     } 

     public String readIt(InputStream stream, int len) throws IOException { 
      Reader reader = null; 
      reader = new InputStreamReader(stream, "UTF-8"); 
      char[] buffer = new char[len]; 
      reader.read(buffer); 
      return new String(buffer); 
     } 

     @Override 
     protected void onPostExecute(String backgroundResult) { 
      super.onPostExecute(backgroundResult); 

      TextView response = (TextView) activity.findViewById(R.id.results); 
      response.setText(backgroundResult); 

     } 
     } 

回答

1

您的上述代碼無法正常工作,因爲您尚未提及針對您的網址的協議,請嘗試使用https://www.google.com而不是www.google.com

+0

謝謝!這是一個簡單的修復! –

相關問題