我想将两个程序“链接”在一起,它们分开工作,一方面我有一个从网上下载的图像库,另一方面我有一个程序可以追踪网址(在一个字符串中)的图像查询ElasticSearch服务器后。
我的目标是在图库中显示查询的结果(=图像)。我想我错过了一步,但我找不到什么步骤。我需要使用AsyncTask吗?
Catlog:
W/dalvikvm(9421): Unable to resolve superclass of Lnet/sf/json/JSONException; (740)
W/dalvikvm(9421): Link of class 'Lnet/sf/json/JSONException;' failed
W/dalvikvm(9421): VFY: unable to resolve exception class 629 (Lnet/sf/json/JSONException;)
W/dalvikvm(9421): VFY: unable to find exception handler at addr 0xff
W/dalvikvm(9421): VFY: rejected Lcom/example/androidgridweb/AndroidGridLayoutActivity;.ESE (Lcom/example/androidgridweb/ImageAdapter;)V
W/dalvikvm(9421): VFY: rejecting opcode 0x0d at 0x00ff
W/dalvikvm(9421): VFY: rejected Lcom/example/androidgridweb/AndroidGridLayoutActivity;.ESE (Lcom/example/androidgridweb/ImageAdapter;)V
W/dalvikvm(9421): Verifier rejected class Lcom/example/androidgridweb/AndroidGridLayoutActivity;
W/dalvikvm(9421): Class init failed in newInstance call (Lcom/example/androidgridweb/AndroidGridLayoutActivity;)
D/AndroidRuntime(9421): Shutting down VM
W/dalvikvm(9421): threadid=1: thread exiting with uncaught exception (group=0x40acb228)
E/AndroidRuntime(9421): FATAL EXCEPTION: main
java.lang.VerifyError: com/example/androidgridweb/AndroidGridLayoutActivity
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1039)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
at android.app.ActivityThread.access$600(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4945)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
AndroidGridLayoutActivity.java
public class AndroidGridLayoutActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
ImageAdapter imgAdapter =new ImageAdapter(this);
imgAdapter.addmThumbIds("http://i724.photobucket.com/albums/ww246/bnhenson/squishy.jpg");
imgAdapter.addmThumbIds("http://xc0.xanga.com/08fe2b7a30c37281496919/m224298958.jpg");
imgAdapter.addmThumbIds("http://cdn4.teen.com/wp-content/uploads/2012/07/paranorman-zayn-malik-tweets.jpg");
try {
eSearchElastic.ESE(imgAdapter);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
gridView.setAdapter(imgAdapter);
}
}
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Context mContext;
Bitmap bmImg;
public static List<String> mThumbIds = new ArrayList<String>();
public void addmThumbIds(String url){
mThumbIds.add(url);
}
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.size();
}
@Override
public Object getItem(int position) {
return mThumbIds.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
downloadFile(imageView, mThumbIds.get(position));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(135, 135));
imageView.setPadding(0, 0, 1, 0);
}
else{
imageView=(ImageView) convertView;
downloadFile(imageView, mThumbIds.get(position));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(135, 135));
imageView.setPadding(0, 0, 1, 0);
}
return imageView;
}
void downloadFile(final ImageView imageView, String fileUrl) {
AsyncTask<Object, Object, String> task = new AsyncTask<Object, Object, String>() {
@Override
protected String doInBackground(Object... params) {
URL myFileUrl = null;
try {
myFileUrl = new URL((String) params[0]);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String unused) {
imageView.setImageBitmap(bmImg);
}
};
task.execute(fileUrl);
}
}
ESearchElastic.java
public class eSearchElastic{
public static void ESE (final ImageAdapter imgAdapter)throws ClientProtocolException, IOException {
String server = "servername";
String index = "images";
String type = "images_schema_1";
StringEntity query = null;
try {
query = new StringEntity("{\"sort\" : [ {\"confidence_level\" : {\"order\" : \"desc\"} }],\"from\" : 0, \"size\" : 10,\"query\" : {\"text_phrase\" : { \"keyword\" : \"finding nemo\"}},\"filter\" : {\"numeric_range\" : {\"confidence_level\" : { \"from\" : 10, \"to\" : 100, \"include_lower\" : true, \"include_upper\" : true}}}}'");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int i=0;
HttpClient httpclient = new DefaultHttpClient();
StringBuilder urlStr = new StringBuilder("http://");
urlStr.append(server)
.append("/" + index)
.append("/" + type + "/_search");
System.out.println(urlStr.toString());
while (i < 5) {
HttpPost httpost = new HttpPost(urlStr.toString());
httpost.setEntity((HttpEntity)query);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");
HttpResponse response = null;
try {
response = httpclient.execute(httpost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String answer = null;
try {
answer = org.apache.http.util.EntityUtils.toString(response.getEntity());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(answer);
i++;
JSONObject answerJson = (JSONObject) JSONSerializer.toJSON(answer);
String urlImage;
try {
urlImage = answerJson.getString("hits");
JSONObject answerJson1 = (JSONObject) JSONSerializer.toJSON(urlImage);
String urlImage1 = answerJson1.getString("hits").replace("[", "").replace("]","");
JSONObject answerJson2 = (JSONObject) JSONSerializer.toJSON(urlImage1);
String urlImage2 = answerJson2.getString("_source");
JSONObject answerJson3 = (JSONObject) JSONSerializer.toJSON(urlImage2);
String urlImage3 = answerJson3.getString("url");
System.out.println(urlImage3);
imgAdapter.addmThumbIds(urlImage3);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
添加/修改:
eSearchElastic.java:
public static void ESE (final ImageAdapter imgAdapter)throws ClientProtocolException, IOException {
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>(){
@Override
protected Void doInBackground(Void... params) {
String server = "esearch.gumgum.com";
String index = "images";
String type = "images_schema_1";
StringEntity query = null;
try {
query = new StringEntity("{\"sort\" : [ {\"confidence_level\" : {\"order\" : \"desc\"} }],\"from\" : 0, \"size\" : 10,\"query\" : {\"text_phrase\" : { \"keyword\" : \"finding nemo\"}},\"filter\" : {\"numeric_range\" : {\"confidence_level\" : { \"from\" : 10, \"to\" : 100, \"include_lower\" : true, \"include_upper\" : true}}}}'");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int i=0;
HttpClient httpclient = new DefaultHttpClient();
StringBuilder urlStr = new StringBuilder("http://");
urlStr.append(server)
.append("/" + index)
.append("/" + type + "/_search");
System.out.println(urlStr.toString());
while (i < 5) {
HttpPost httpost = new HttpPost(urlStr.toString());
httpost.setEntity((HttpEntity)query);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");
HttpResponse response = null;
try {
response = httpclient.execute(httpost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String answer = null;
try {
answer = org.apache.http.util.EntityUtils.toString(response.getEntity());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(answer);
i++;
JSONObject answerJson = (JSONObject) JSONSerializer.toJSON(answer);
String urlImage;
try {
urlImage = answerJson.getString("hits");
JSONObject answerJson1 = (JSONObject) JSONSerializer.toJSON(urlImage);
String urlImage1 = answerJson1.getString("hits").replace("[", "").replace("]","");
JSONObject answerJson2 = (JSONObject) JSONSerializer.toJSON(urlImage1);
String urlImage2 = answerJson2.getString("_source");
JSONObject answerJson3 = (JSONObject) JSONSerializer.toJSON(urlImage2);
String urlImage3 = answerJson3.getString("url");
System.out.println(urlImage3);
imgAdapter.addmThumbIds(urlImage3);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
};
task.execute();
}
新Catlog:
E/AndroidRuntime(10619): FATAL EXCEPTION: main
E/AndroidRuntime(10619): java.lang.VerifyError: com/example/androidgridweb/eSearchElastic$1
at com.example.androidgridweb.eSearchElastic.ESE(eSearchElastic.java:28)
at com.example.androidgridweb.AndroidGridLayoutActivity.onCreate(AndroidGridLayoutActivity.java:44)
at android.app.Activity.performCreate(Activity.java:4531)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
at android.app.ActivityThread.access$600(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4945)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
Android&gt; = 3.0在主UI线程上不允许Network Request
。
您需要使用AsyncTask来呼叫网络请求。
答案 1 :(得分:0)
我发现了错误。问题是库net.sf.json,我使用的是Android SDK JSON实现。
很抱歉在发布我的问题之前找不到这个。
我只是替换了我的“json部分”:
JSONObject answerJson = new JSONObject(answer);
String urlImage;
urlImage = answerJson.getString("hits");
JSONObject answerJson1 = new JSONObject(urlImage);
String urlImage1 = answerJson1.getString("hits").replace("[", "").replace("]","");
JSONObject answerJson2 = new JSONObject(urlImage1);
String urlImage2 = answerJson2.getString("_source");
JSONObject answerJson3 = new JSONObject(urlImage2);
String urlImage3 = answerJson3.getString("url");
System.out.println(urlImage3);
imgAdapter.addmThumbIds(urlImage3);