2015-12-14 4 views
6

मुझे खेद है, मेरा प्रश्न केवल एक पंक्ति दिखाई देता है और मेरी परियोजना में image_url1,2,3,4,5 इरादे से दूसरी गतिविधि पर कार्य करता है, और यह पहले से ही काम करता है मैं छवि अपलोड नहीं कर सकते हैं, चित्र के नीचे देखने के लिए कृपयाएंड्रॉइड छविबट्टन पर JSON सरणी प्रतिक्रिया ऑनक्लिक

┌──────┐┌──────┐┌──────┐┌──────┐┌──────┐ 
│■■■■■■││  ││  ││  ││  │ 
│■■■■■■││  ││  ││  ││  │ 
└──────┘└──────┘└──────┘└──────┘└──────┘ 
only first url parsing works another didn't.... 
(first box parsed to image_url, when it clicks image_url1,2,3,4,5 appear) 
i want all rows appear, not one row 

से पहले मैं सवाल पूछ, मैं अधिक अंग्रेजी सीखने की जरूरत है ......


मैं पर इन json सरणी प्रतिक्रिया चाहते हैं मेरा आवेदन imagebuttons (या ग्रिडव्यू) मैं से छवियों की एक सरणी प्राप्त था और बटन json सरणी के रूप में प्रत्येक पंक्ति डेटा अधिनियम मेरी कक्षा डेटा

this is my activity about receive data 

private class SearchThread implements Runnable { 

    @Override 
    public void run() { 
     try { 
      HttpClient client = new DefaultHttpClient(); 
      HttpPost post = new HttpPost(imageSearchUrl); 
      MultipartEntity reqEntity = new MultipartEntity(); 
      StringBody part1 = new StringBody(imageId, Charset.forName("UTF-8")); 
      reqEntity.addPart("imageId", part1); 
      //pages =1 :0-2 2:5-6 3:6-8 
      StringBody pages = new StringBody("1"); 
      reqEntity.addPart("pages", pages); 

      post.setEntity(reqEntity); 
      post.setHeader("enctype", "multipart/form-data;"); 
      HttpResponse response = client.execute(post); 
      int statusCode = response.getStatusLine().getStatusCode(); 
      if (statusCode == 200) {// success 

       HttpEntity entity = response.getEntity(); 
       String resJson = EntityUtils.toString(entity); 
       System.out.println("**** = " + resJson); 
       jsonStr = resJson; 
       JSONTokener jsonParser = new JSONTokener(resJson); 
       JSONObject itemList = (JSONObject) jsonParser.nextValue(); 
       int total = itemList.getInt("total"); 
       int currentPage = itemList.getInt("currentPage"); 
       JSONArray jsonObjs = itemList.getJSONArray("rows"); 
       String demoUrl = ""; 
       String s = ""; 
       List<String> imageUrlList = new ArrayList<String>(); 
       for (int i = 0; i < jsonObjs.length(); i++) { 
        JSONObject jsonObj = jsonObjs.getJSONObject(i); 
        // String id = jsonObj.getInt("id"); 
        String image_name = jsonObj.getString("image_name"); 
        String image_url = jsonObj.getString("image_url"); 
        String image_url1 = jsonObj.getString("image_url1"); 
        String image_url2 = jsonObj.getString("image_url2"); 
        String image_url3 = jsonObj.getString("image_url3"); 
        String image_url4 = jsonObj.getString("image_url4"); 
        String image_url5 = jsonObj.getString("image_url5"); 
        String image_url6 = jsonObj.getString("image_url6"); 
        String price = jsonObj.getString("price"); 
        imageUrlList.add(image_url); 

        // s += " image_name = " + image_name + "image_url = " + 
        // image_url; 

        if (i == 0) { 
         urlStr = image_url1 +","+ image_url2 +","+ image_url3 +","+ 
           image_url4 +","+ image_url5 +","+ image_url6; 
         demoUrl = image_url; 
         System.out.println("########### " + image_url1 + " ---" + image_url2 + "---" + image_url3 + " ---" + image_url4 + "---" + image_url5 + " ---" 
           + image_url6 + "---"); 
        } 
       } 
       String s1 = demoUrl.replaceAll("127.0.0.1", "url"); 

       mHandler.obtainMessage(0, s1).sendToTarget(); 
      } else { 

      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
      System.out.println("%%%%%%%%%%%5" + e.toString()); 
     } 

    } 

} 

public String uploadImage(String url, String filepath) { 
    File file = new File(filepath); 

    if (!file.exists()) { 
     return null; 
    } 

    HttpClient client = new DefaultHttpClient(); 
    HttpPost post = new HttpPost(url); 

    FileBody fileBody = new FileBody(file, "image/jpeg"); 
    MultipartEntity entity = new MultipartEntity(); 
    entity.addPart("image", fileBody); 

    post.setEntity(entity); 

    try { 
     HttpResponse response = client.execute(post); 
     int statusCode = response.getStatusLine().getStatusCode(); 
     String result = EntityUtils.toString(response.getEntity(), "utf-8"); 

     if (statusCode == 201) { 
      // upload success 
      // do something 
     } 

     return result; 
    } catch (Exception e) { 
     System.out.println(e.toString()); 
    } 

    return null; 
} 

private Handler mHandler = new Handler() { 
    public void handleMessage(Message msg) { 
     switch (msg.what) { 
     case 0: 
      try { 
       URL url = new URL(msg.obj.toString()); 
       et3.setText(msg.obj.toString()); 
       new Thread(new ImageRunnable()).start(); 
      } catch (Exception e) { 
       System.out.println("^^^^^^^" + e.toString()); 
      } 

      break; 

     case 1: 
      Toast.makeText(getApplication(), "failed", Toast.LENGTH_LONG).show(); 
      break; 
     } 
    } 
}; 

private class ImageRunnable implements Runnable { 
    @Override 
    public void run() { 
     // get the image by use url 
     HttpClient hc = new DefaultHttpClient(); 
     HttpGet hg = new HttpGet(et3.getText().toString()); 
     final Bitmap bm; 
     try { 
      HttpResponse hr = hc.execute(hg); 
      bm = BitmapFactory.decodeStream(hr.getEntity().getContent()); 
     } catch (Exception e) { 
      mHandler2.obtainMessage(1).sendToTarget(); 
      return; 
     } 
     mHandler2.obtainMessage(0, bm).sendToTarget(); 
    } 
}; 

private Handler mHandler2 = new Handler() { 
    public void handleMessage(Message msg) { 
     switch (msg.what) { 
     case 0: 
      //ImageView iv2 = (ImageView) findViewById(R.id.imageView2); 
     // iv2.setImageBitmap((Bitmap) msg.obj);// 
      imageButton1.setImageBitmap((Bitmap) msg.obj); 
      Toast.makeText(getApplication(), "success", Toast.LENGTH_LONG).show(); 
      break; 

     case 1: 
      Toast.makeText(getApplication(), "failed", Toast.LENGTH_LONG).show(); 
      break; 
     } 
    } 
}; 

@Override 
public boolean onKey(View v, int keyCode, KeyEvent event) { 
    switch (event.getAction()) { 
    case KeyEvent.ACTION_UP: { 

    } 
    case KeyEvent.ACTION_DOWN: { 


    } 
    default: 
     break; 
    } 
    return false; 
} 

मेरी xml कोड प्राप्त है यहाँ यहाँ है

{ 
"total" : 2, 
"row" : [ 
    { 
     "id": "c3asfasfas35sd4a35as5d4a3", 
     "image_name": "20150913151562135", 
     "image_url": "http://myurl/imagelocation.jpg", 
     "flag": null, 
     "price": "1200000", 
     "image_url1": "http://image_url1/imagelocation.jpg", 
     "image_url2": "http://image_url2/imagelocation.jpg", 
     "image_url3": "http://image_url3/imagelocation.jpg", 
     "image_url4": "http://image_url4/imagelocation.jpg", 
     "image_url5": "http://image_url5/imagelocation.jpg", 
     "image_url6": "http://image_url6/imagelocation.jpg", 
}, 
{ 
     "id": "c3asfasfas35sd4a35as5d4a3", 
     "image_name": "20150913151562135", 
     "image_url": "http://myurl/imagelocation.jpg", 
     "flag": null, 
     "price": "1200000", 
     "image_url1": "http://image_url7/imagelocation.jpg", 
     "image_url2": "http://image_url8/imagelocation.jpg", 
     "image_url3": "http://image_url9/imagelocation.jpg", 
     "image_url4": "http://image_url10/imagelocation.jpg", 
     "image_url5": "http://image_url11/imagelocation.jpg", 
     "image_url6": "http://image_url12/imagelocation.jpg", 
} 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 
<LinearLayout 
    android:id="@+id/linearLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/linearLayout1" 
    android:layout_alignParentTop="true" 
    > 

    <ImageButton 
     android:id="@+id/picButton" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:padding="0sp" 
     android:scaleType="centerCrop" 
     android:background="@drawable/ic_launcher1" /> 

</LinearLayout> 


<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/linearLayout2"> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Get" 
     /> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.66" 
     android:text="c2db6c9be8e5407c8a226ba8a0851368" 
     android:visibility="gone" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/linearLayout2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/linearLayout5" > 


    <EditText 
     android:id="@+id/editText2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:background="@android:color/white" 
     android:ems="10" 
     android:paddingTop="33px" 
     android:inputType="textMultiLine" 
     android:hint="comment" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/linearLayout3" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:visibility="gone"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="IP:" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="" 
     /> 
</LinearLayout> 

<EditText 
    android:id="@+id/editText3" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:visibility="gone" > 

    <requestFocus /> 
</EditText> 

<LinearLayout 
    android:id="@+id/linearLayout4" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:orientation="vertical" 
    android:weightSum="10" 
    android:paddingBottom="59px" 
    android:paddingTop="10dp"> 


    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="3" 
     android:text="POST" 
     android:paddingLeft="100px" 
     android:paddingRight="100px" 
     android:textStyle="bold" 
     android:textAlignment="center" 
     android:textSize="60px" 
     android:textColor="@android:color/white" 
     /> 

</LinearLayout> 

<LinearLayout 
    android:id="@+id/linearLayout5" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/linearLayout4" 
    android:layout_alignParentLeft="true" 
    android:orientation="horizontal" > 


    <ImageButton 
     android:id="@+id/resultButton1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher1" /> 
    <ImageButton 
     android:id="@+id/resultButton2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher1"/> 
    <ImageButton 
     android:id="@+id/resultButton3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher1" /> 
    <ImageButton 
     android:id="@+id/resultButton4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher1" /> 
    <ImageButton 
     android:id="@+id/resultButton5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher1" /> 
</LinearLayout> 

लेकिन मैं इस कोड को आजमाता हूं, केवल पहला चित्र शो, दूसरानहीं दिखाई देतामेरी मदद करो !!

केवल एक डेटा शीट (एक पंक्ति) प्राप्त होती है सफलता प्राप्त होती है। मुझे नहीं पता कि कुल पंक्ति डेटा कैसे प्राप्त करें .... कृपया मेरी मदद करें। बचाने मेरे जीवन कृपया

+1

क्योंकि आप imageUrlList में केवल एक image_url जोड़ते हैं। –

+0

आप सूची – Dhina

+1

पर केवल 'image_url' जोड़ते हैं यदि आप सूचीदृश्य में सभी image_url जोड़ना चाहते हैं तो आपको ऐसा करना होगा। imageUrlList.add (image_url); imageUrlList.add (image_url1); imageUrlList.add (image_url2); imageUrlList.add (image_url3); –

उत्तर

0

इस संहिता अपना काम प्रयास करें ..

 HttpEntity entity = response.getEntity(); 
       String resJson = EntityUtils.toString(entity); 
       System.out.println("**** = " + resJson); 
       jsonStr = resJson; 
       JSONTokener jsonParser = new JSONTokener(resJson); 
       JSONObject itemList = (JSONObject) jsonParser.nextValue(); 
       int total = itemList.getInt("total"); 
       int currentPage = itemList.getInt("currentPage"); 
       JSONArray jsonObjs = itemList.getJSONArray("rows"); 
       String demoUrl = ""; 
       String s = ""; 
       List<String> imageUrlList = new ArrayList<String>(); 
       for (int i = 0; i < jsonObjs.length(); i++) { 
        JSONObject jsonObj = jsonObjs.getJSONObject(i); 
        // String id = jsonObj.getInt("id"); 
        String image_name = jsonObj.getString("image_name"); 
        String image_url = jsonObj.getString("image_url"); 
        String image_url1 = jsonObj.getString("image_url1"); 
        String image_url2 = jsonObj.getString("image_url2"); 
        String image_url3 = jsonObj.getString("image_url3"); 
        String image_url4 = jsonObj.getString("image_url4"); 
        String image_url5 = jsonObj.getString("image_url5"); 
        String image_url6 = jsonObj.getString("image_url6"); 
        String price = jsonObj.getString("price"); 
        imageUrlList.add(image_url); 
        imageUrlList.add(image_url1); 
        imageUrlList.add(image_url2); 
        imageUrlList.add(image_url3); 
        imageUrlList.add(image_url4); 
        imageUrlList.add(image_url5); 
        imageUrlList.add(image_url6); 
+0

आपकी टिप्पणी के लिए धन्यवाद, लेकिन कृपया मेरे प्रश्न को फिर से पढ़ें – plzunderstandmyenglish

0

क्योंकि आप अपनी सूची में सिर्फ एक image_url जोड़ें।

आप सभी यूआरएल की सूची में आप नीचे दिए गए की तरह करना है,

imageUrlList.add(image_url); 
imageUrlList.add(image_url1); 
imageUrlList.add(image_url2); 
imageUrlList.add(image_url3); 
imageUrlList.add(image_url4); 
imageUrlList.add(image_url5); 
imageUrlList.add(image_url6); 

लेकिन इसके बजाय इस बात का,

बेहतर तरीका है अपने सभी छवियाँ की JSONArray बनाने के लिए है जोड़ना चाहते हैं।

+0

आपकी टिप्पणी के लिए धन्यवाद, मुझे खेद है कि मेरा प्रश्न गलत था मेरी समस्या सिर्फ एक छवि है और url1,2,3,4,5 दिखाई दे मैं अपने qusetion को सही कर दूंगा – plzunderstandmyenglish

संबंधित मुद्दे