JSON没有回应

时间:2016-01-18 09:15:42

标签: android json android-volley

我正在尝试使用volley从服务器中的文件夹中检索图像。该应用程序工作一次,但是当我从android studio运行应用程序时突然间,我没有得到JSON响应而且我得到一个空白的屏幕

package com.example.alex.imagerequestapp;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;

public class MainActivity extends AppCompatActivity {
ImageView mImageView;
String url;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //refernce both the image and the url to the image in the netwrok folder
    mImageView = (ImageView) findViewById(R.id.myImage);
    url = "http://192.168.56.1:80/bio_data_test/images/summerfestival.jpg";

    //begin the ImageRequest
    ImageRequest request = new ImageRequest(url,
            new Response.Listener<Bitmap>() {
                @Override
                public void onResponse(Bitmap bitmap) {
                    mImageView.setImageBitmap(bitmap);
                }
            }, 0, 0, null,
            new Response.ErrorListener() {
                public void onErrorResponse(VolleyError error) {

                }
            });
    // Access the RequestQueue through your AppConroller class.
   // AppController.getmInstance().addToRequestQueue(request);
    AppController.getmInstance().addToRequestQueue(request);


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@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);
  }
 }

2 个答案:

答案 0 :(得分:0)

如果您使用的是模拟器且您有本地服务器,请使用此IP地址10.0.2.2

您可以使用Picasso库轻松地将图像从url加载到ImageView,它会为您处理网络呼叫

示例:

Picasso.with(getApplicationContext()).load(image_url).into(mImageView);


where, 
mImageView ===> is your imageView instance after 
image_url ===> url of the image on the server 

添加Picasso库,将以下代码添加到build.gradle文件中的依赖项块,如下所示

compile 'com.squareup.picasso:picasso:2.5.2'

您可以使用picasso

答案 1 :(得分:0)

我已经能够解决此问题..网址从192.168.56.1/bio_data_test/images/summerfestival.jpg更改为192.168.56.2/bio_data_test/images/summerfestival.jpg。我在cmd上运行ipconfig后发现了这个。我更改了IP地址后,它工作了。