1

サーバーから画像(jpg)とjsonオブジェクトをダウンロードするAndroidアプリを作成したいと思います。このために、2 つの Async Task を作成しました。ただし、2 つのタスクを同時に呼び出すと、最初のタスクのみが読み込まれ、2 番目のタスクは読み込まれません。誰かが問題を説明して助けてくれますか?

これが私のコードです:

public class Catalog extends Activity {
private TextView text;
private ImageButton image;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_catalog);
   // Intent intent = getIntent();

    /*RelativeLayout m_vwJokeLayout=(RelativeLayout) this.findViewById(R.id.layout1);
    LayoutParams lparams = new LayoutParams(
               LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            TextView tv=new TextView(this);
            tv.setLayoutParams(lparams);
            tv.setText("test");
            m_vwJokeLayout.addView(tv);*/

    image = (ImageButton) findViewById(R.id.imageButton1);
    final String imageUrl="http://137.189.97.18/image/20000338902.jpg";

    text = (TextView) findViewById(R.id.textView1);
    Spinner s = (Spinner) findViewById(R.id.spinner1);
    String[] showcata = getResources().getStringArray(R.array.CatalogSpinner);
    ArrayAdapter <String> adapter = new ArrayAdapter <String>(this,android.R.layout.simple_spinner_item, showcata);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s.setAdapter(adapter);

    s.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            String selected = parentView.getSelectedItem().toString();
            text.setText(selected);

            //text.setBackgroundColor(getResources().getColor(R.color.Blue));
            /*RelativeLayout m_vwJokeLayout=(RelativeLayout) Catalog.this.findViewById(R.id.layout1);
            LayoutParams lparams = new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            TextView tv=new TextView(Catalog.this);
            tv.setLayoutParams(lparams);
            tv.setText(selected);
            m_vwJokeLayout.addView(tv);*/

            //AsyncTask myTask = new httpconnect(Catalog.this);

            Catalog.httpconnect myTask = new httpconnect(Catalog.this);
            myTask.execute("123");

            Catalog.downloadimage download = new downloadimage(Catalog.this);
            download.execute(imageUrl);
    /*if(selected.equals("Pants"))
        text.setText("RIGHT");*/
        }

        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }

    });

}

public static class httpconnect extends AsyncTask<String, Void, String>{
    public Catalog myActivity;
    public httpconnect(Catalog a)
    {
        myActivity = a;
    }

protected void onPreExecute(){
 //text.setText("load");
}

protected String doInBackground(String... x){
    String result = null;
    InputStream is = null;
    StringBuilder sb=null;

    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://137.189.97.18/main2.php");
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("_method", "post"));
        nameValuePairs.add(new BasicNameValuePair("catagory", x[0]));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

    }catch(Exception e){

        Log.d("HTTP response error",e.toString());
    }


    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        sb = new StringBuilder();
        sb.append(reader.readLine() + "\n");

        String line="0";
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result=sb.toString();

    }catch(Exception e){
    }
    return result;

}

protected void onProgressUpdate(){

}

protected void onPostExecute(String z){
    //text.setText(z);
    Log.d("BAC", z);
    myActivity.text.setText(z);

    String product_id = null;
    String colour_id = null;
    String colour= null;
    String picture= null;

        JSONObject json_data;
        try {
            json_data = new JSONObject(z.trim());
            product_id=json_data.getString("product_id");
            colour_id=json_data.getString("price");
            colour=json_data.getString("colour");
            picture=json_data.getString("picture");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            JSONArray jArray;
            try {
                jArray = new JSONArray(z.trim());
                 JSONObject json_data2=null;
                    for(int i=0;i<jArray.length();i++){
                            json_data2 = jArray.getJSONObject(i);
                            product_id=json_data2.getString("product_id");
                            colour_id=json_data2.getString("colour_id");
                            colour=json_data2.getString("colour");
                            picture=json_data2.getString("picture");
                    }
            } catch (JSONException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            //e.printStackTrace();
        }


    TextView text2 = (TextView) myActivity.findViewById(R.id.textView2);
    myActivity.text.setText(product_id);
    text2.setText(colour_id);

}
}

public static class downloadimage extends AsyncTask<String, Void, Bitmap>{
    public Catalog myActivity;
    public downloadimage(Catalog a)
    {
        myActivity = a;
    }

protected void onPreExecute(){
 //text.setText("load");
}

protected Bitmap doInBackground(String... x){
    Bitmap bmImg = null;
    URL myFileUrl =null; 
    try {
    myFileUrl= new URL(x[0]);
    } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
    conn.setDoInput(true);
    conn.connect();
    int length = conn.getContentLength();
    int[] bitmapData =new int[length];
    byte[] bitmapData2 =new byte[length];
    InputStream is = conn.getInputStream();

    bmImg = BitmapFactory.decodeStream(is);

    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return bmImg;


}

protected void onProgressUpdate(){

}

protected void onPostExecute(Bitmap z){
    //text.setText(z);
    //Log.d("image", z);

    myActivity.image.setImageBitmap(z);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_catalog, menu);
    return true;
}

}

4

0 に答える 0