Android 如何从json响应中获取图像?
您好,我正在尝试在我的应用程序活动中使用JSON从响应中获取数据。我获得了除图像之外的所有数据,它没有显示在我的输出中。我不知道使用简单的Android 如何从json响应中获取图像?,android,json,Android,Json,您好,我正在尝试在我的应用程序活动中使用JSON从响应中获取数据。我获得了除图像之外的所有数据,它没有显示在我的输出中。我不知道使用简单的setTag和setText方法有什么问题,有人能帮忙吗 public class ProfilePage extends Activity{ private ProgressDialog pDialog; //private static final String TAG_MATCH_ID="match_detail_id"; private stat
setTag
和setText
方法有什么问题,有人能帮忙吗
public class ProfilePage extends Activity{
private ProgressDialog pDialog;
//private static final String TAG_MATCH_ID="match_detail_id";
private static final String USER_NAME="name";
private static final String USER_AGE="age";
private static final String USER_LOCATION="location";
private static final String USER_MOTHER_TONGE="mother_tounge";
private static final String USER_OCCU="occupation";
private static final String USER_INCOM="income";
private static final String USER_HEIGHT="height";
private static final String USER_MARRAGE="marital_status";
private static final String USER_RELIGION="religion";
private static final String USER_GOTRA="gotra";
private static final String USER_MANGLIK="manglik";
private static final String USER_RASHI="rashi";
private static final String USER_EDUCATION="education";
private static final String USER_EAT="eating";
private static final String USER_DRINK="drink";
private static final String USER_SMOKE="smoke";
private static final String USER_ABOUT="about_me";
private static final String USER_PIC="profile_pic";
private static String USER_URL="http://abcfg.com/webservice/matchingdetails?version=apps&match_detail_id=636";
String user_match_id;
private ImageView cover;
private ImageView yes;
private ImageView no;
private ImageView sendmsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_page);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(USER_URL, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
try {
JSONObject jsonObj = new JSONObject(jsonStr);
String user_name = jsonObj.getString(USER_NAME);
String user_age = jsonObj.getString(USER_AGE);
String user_location = jsonObj.getString(USER_LOCATION);
String user_mothertong = jsonObj.getString(USER_MOTHER_TONGE);
String user_occupation = jsonObj.getString(USER_OCCU);
String user_income = jsonObj.getString(USER_INCOM);
String user_height = jsonObj.getString(USER_HEIGHT);
String user_marg = jsonObj.getString(USER_MARRAGE);
String user_religion = jsonObj.getString(USER_RELIGION);
String user_gotra = jsonObj.getString(USER_GOTRA);
String user_manglik = jsonObj.getString(USER_MANGLIK);
String user_rashi = jsonObj.getString(USER_RASHI);
String user_education = jsonObj.getString(USER_EDUCATION);
String user_eat = jsonObj.getString(USER_EAT);
String user_drink = jsonObj.getString(USER_DRINK);
String user_smoke = jsonObj.getString(USER_SMOKE);
String user_about = jsonObj.getString(USER_ABOUT);
String user_pro = jsonObj.getString(USER_PIC);
final TextView uname = (TextView)findViewById(R.id.namedetail);
final TextView fdetail = (TextView)findViewById(R.id.firstdetail);
final TextView sdetail = (TextView)findViewById(R.id.seconddetail);
final TextView tdetail = (TextView)findViewById(R.id.thirddetail);
final TextView ocdetail=(TextView)findViewById(R.id.txtoccupationdetail);
final TextView incomedetail = (TextView)findViewById(R.id.incomedetaile);
final TextView uheight = (TextView)findViewById(R.id.txtheightprofile);
final TextView umrg = (TextView)findViewById(R.id.txtmrgprofile);
final TextView ureligion = (TextView)findViewById(R.id.prohindu);
final TextView ugotra = (TextView)findViewById(R.id.gothraa);
final TextView umanglik = (TextView)findViewById(R.id.usermanglik);
final TextView urashi = (TextView)findViewById(R.id.rashi);
final TextView udegree = (TextView)findViewById(R.id.userdegree);
final TextView ueat = (TextView)findViewById(R.id.txteatprofile);
final TextView udrink = (TextView)findViewById(R.id.txtdrinkprofile);
final TextView usmoke = (TextView)findViewById(R.id.txtsmokeprofile);
final TextView uabout = (TextView)findViewById(R.id.txtabouther);
final ImageView ucover = (ImageView)findViewById(R.id.coverimage);
uname.setText(user_name);
fdetail.setText(user_age+" years");
sdetail.setText(user_location);
tdetail.setText(user_mothertong);
ocdetail.setText(user_occupation);
incomedetail.setText(user_income);
uheight.setText(user_height);
umrg.setText(user_marg);
ureligion.setText(user_religion);
ugotra.setText(user_gotra);
umanglik.setText(user_manglik);
urashi.setText(user_rashi);
udegree.setText(user_education);
ueat.setText(user_eat);
udrink.setText(user_drink);
usmoke.setText(user_smoke);
uabout.setText(user_about);
ucover.setTag(user_pro);
} catch (JSONException e) {
e.printStackTrace();
}
使用离子库。。。。从网络上获取图像非常简单。。。。像这样,
试试这个
我认为你们是从url下载图片,所以我建议你们使用Android查询,若你们使用它,你们将永远不会得到这个错误 您可以从这里下载jar文件:下载jar文件并将jar设置为构建路径
AQuery androidAQuery=new AQuery(this);
以直接从url加载图像为例:
androidAQuery.id(ucover).image(user_pro, true, true);
androidAQuery.ajax(YOUR IMAGE URL,Bitmap.class,0,new AjaxCallback<Bitmap>(){
@Override
public void callback(String url, Bitmap object, AjaxStatus status) {
super.callback(url, object, status);
//You will get Bitmap from object.
}
});
作为从url获取位图的示例:
androidAQuery.id(ucover).image(user_pro, true, true);
androidAQuery.ajax(YOUR IMAGE URL,Bitmap.class,0,new AjaxCallback<Bitmap>(){
@Override
public void callback(String url, Bitmap object, AjaxStatus status) {
super.callback(url, object, status);
//You will get Bitmap from object.
}
});
AndroidQuery.ajax(您的图像URL、位图.class、0、新的AjaxCallback(){
@凌驾
公共无效回调(字符串url、位图对象、AjaxStatus状态){
super.callback(url、对象、状态);
//您将从对象中获取位图。
}
});
这是非常快速和准确的,使用它你可以找到更多的功能,如动画加载时;如果需要,获取位图;等等。以下是可行的解决方案: 首先使用以下命令从JSON获取图像的URL-
String imgURL = json.getString(JSON_TAG_OF_IMAGE); //Getting the URL
现在,使用以下代码从URL获取图像(用try/catch包围)
以下是getBitmapFromurl:--
就这样。您可以将位图(照片变量)设置为imageview。
希望这有帮助:):)在显示图像的地方,我看不到任何代码。最后一个字段是在我的codeucover.setTag(user_pro)中看到的图像;这是显示图像的代码吗?是的,我是这段代码,但图像不显示Json返回的图像格式是什么?它是一个图像的路径还是一些图像字节数据?您必须首先添加库ION和GSON,然后简单地将此代码添加到MainActivity.javaso中,我应该在其中添加jar文件download@Eddie列表中的第一个,确定,但它显示AjaxStatus错误并请求创建类AjaxStatus无法解析为type@Eddie不要使用该代码,只需使用这一行:androidquery.id(ucover.image)(user\u pro,true,true);那个让我们排除这个问题
private static Bitmap getBitmapFromUrl(String hos_imgURL)
throws IOException {
URL IMGURL = new URL(hos_imgURL);
Log.d(_TAG, "Image Url:" + IMGURL.toString());
HttpURLConnection conn = (HttpURLConnection) IMGURL.openConnection();
conn.setDoInput(true);
conn.connect();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
InputStream is = conn.getInputStream();
bmp = BitmapFactory.decodeStream(is);
return bmp;
}