2016-12-20 210 views
0

應用程序的作品(api 23測試也kitkat測試),但沒有顯示,它應該顯示圖片(下載並放在視圖中)。由於我沒有得到任何錯誤,我迷路了。我正在從Android初學者書中尋找代碼,他們建議應用程序在4.0(冰淇淋)上進行測試,但是沒有推薦在該版本的android studio中構建。這是不是工作,因爲Android的最新的API與HTTP和網絡的工作方式發生了一些變化?Android網絡簡單的應用程序

public class NetworkingActivity extends Activity { 

ImageView img; 

private InputStream OpenHttpConnection(String urlString) 
     throws IOException 
{ 
    InputStream in = null; 
    int response = -1; 

    URL url = new URL(urlString); 
    URLConnection conn = url.openConnection(); 

    if (!(conn instanceof HttpURLConnection)) 
     throw new IOException("Not an HTTP connection"); 
    try{ 
     HttpURLConnection httpConn = (HttpURLConnection) conn; 
     httpConn.setAllowUserInteraction(false); 
     httpConn.setInstanceFollowRedirects(true); 
     httpConn.setRequestMethod("GET"); 
     httpConn.connect(); 
     response = httpConn.getResponseCode(); 
     if (response == HttpURLConnection.HTTP_OK) { 
      in = httpConn.getInputStream(); 
     } 
    } 
    catch (Exception ex) 
    { 
     Log.d("Networking", ex.getLocalizedMessage()); 
     throw new IOException("Error connecting"); 
    } 
    return in; 
} 

private Bitmap DownloadImage(String URL) 
{ 
    Bitmap bitmap = null; 
    InputStream in = null; 
    try { 
     in = OpenHttpConnection(URL); 
     bitmap = BitmapFactory.decodeStream(in); 
     in.close(); 
    } catch (IOException e1) { 
     Log.d("NetworkingActivity", e1.getLocalizedMessage()); 
    } 
    return bitmap; 
} 

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
    protected Bitmap doInBackground(String... urls) { 
     return DownloadImage(urls[0]); 
    } 

    protected void onPostExecute(Bitmap result) { 
     ImageView img = (ImageView) findViewById(R.id.img); 
     img.setImageBitmap(result); 
    } 
} 

public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    new DownloadImageTask().execute("http://www.mayoff.com/5-01cablecarDCP01934.jpg"); 

} 
} 

這裏是logcat的:

logcat

AndroidManifest.xml文件:

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

<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"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

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

MainActivity類別:

package metropolitan.com.desetopredavanjeprotokoli10; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 
} 

在這裏你去:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Smor" 
    android:layout_gravity="center"/> 

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/img" 
    android:layout_gravity="center"/> 
</RelativeLayout> 
+1

R.layout.activity_main我需要這個=。= –

回答

1

既然你是beginer我建議你學習如何使用框架 有使用框架,如果不是你將需要實現一些複雜的代碼,用於處理諸如緩存有很大的好處,調整大小,避免內存崩潰,處理幾乎所有的API,你所面臨的等

對於圖像下載我建議

Picasso(這是我最建議)

Glide

對於普通的Http請求

Volley

您可以我們抽射下載映像。當你開始學習時,這很難理解,但是當你真正懂得了之後,我保證你不會使用普通的HTTPURLConnection來處理任何HTTP請求。

如果你需要上傳,請告訴我,我會給你添加代碼也是如此,雖然它很複雜。

編輯:

到目前爲止,我已經測試出你的代碼它的工作完美fine.I想添加它。

MainActivity

ImageView img; 

    private InputStream OpenHttpConnection(String urlString) 
      throws IOException 
    { 
     InputStream in = null; 
     int response = -1; 

     URL url = new URL(urlString); 
     URLConnection conn = url.openConnection(); 

     if (!(conn instanceof HttpURLConnection)) 
      throw new IOException("Not an HTTP connection"); 
     try{ 
      HttpURLConnection httpConn = (HttpURLConnection) conn; 
      httpConn.setAllowUserInteraction(false); 
      httpConn.setInstanceFollowRedirects(true); 
      httpConn.setRequestMethod("GET"); 
      httpConn.connect(); 
      response = httpConn.getResponseCode(); 
      if (response == HttpURLConnection.HTTP_OK) { 
       in = httpConn.getInputStream(); 
       Log.d("asd","OK"); 
      }else{ 
       Log.d("asd","error"); 
      } 
     } 
     catch (Exception ex) 
     { 
      Log.d("Networking", ex.getLocalizedMessage()); 
      throw new IOException("Error connecting"); 
     } 
     return in; 
    } 

    private Bitmap DownloadImage(String URL) 
    { 
     Bitmap bitmap = null; 
     InputStream in = null; 
     try { 
      in = OpenHttpConnection(URL); 
      Log.d("in", String.valueOf(in)); 
      bitmap = BitmapFactory.decodeStream(in); 
      in.close(); 
     } catch (IOException e1) { 
      Log.d("NetworkingActivity", e1.getLocalizedMessage()); 
     } 
     return bitmap; 
    } 

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 

     protected Bitmap doInBackground(String... urls) { 
      Log.d("aSD","ASD"); 
      return DownloadImage(urls[0]); 
     } 

     protected void onPostExecute(Bitmap result) { 
      ImageView img = (ImageView) findViewById(R.id.img); 
      img.setImageBitmap(result); 
     } 
    } 

    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     new DownloadImageTask().execute("http://www.mayoff.com/5-01cablecarDCP01934.jpg"); 

    } 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.myapplication.MainActivity"> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/img"/> 
</RelativeLayout> 

末添加此體現

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

結果 enter image description here

如果確實無法正常工作,請考慮檢查您的仿真器或設備網絡連接。

+0

嗯,tnx爲此。我會檢查那些肯定的。同樣在這個日誌中,你知道什麼可能導致圖片無法顯示嗎? Tnx再次爲方向,我會開始學習他們一旦我完成這個錯誤的應用程序。這個應用程序是家庭作業,所以我需要完成它。 – Beansolder

+0

@Beansolder是否在清單中添加了許可? –

+0

是在清單文件中我添加了INTERNET權限。 – Beansolder