2012-05-14 5 views
0

私はただ非同期タスクを介してデモアプリケーションをビルドし、リストビューでjsonデータを望むので、json関数と配列リストなどを追加できる場所はわかりませんplzは私を案内したり、事前に編集することにより、私感謝のコードを、私を助け、plzはAndroidとJavaへイム新しいのを助けるAsyncTaskを使って私は安らかなAPIを呼び出すときリストビューのデータをしたい

package your.packag.namespace; 
import java.io.IOException; 
import java.io.InputStream; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.protocol.BasicHttpContext; 
import org.apache.http.protocol.HttpContext; 
import android.app.Activity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 



public class runActivity extends Activity implements OnClickListener { 


@Override 


    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     findViewById(R.id.my_button).setOnClickListener(this); 
} 

@Override 
public void onClick(View arg0) { 
    Button b = (Button)findViewById(R.id.my_button); 
    b.setClickable(false); 
    new LongRunningGetIO().execute(); 
} 

private class LongRunningGetIO extends AsyncTask <Void, Void, String> { 

    protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException { 
     InputStream in = entity.getContent(); 
     StringBuffer out = new StringBuffer(); 
     int n = 1; 
     while (n>0) { 
      byte[] b = new byte[4096]; 
      n = in.read(b); 
      if (n>0) out.append(new String(b, 0, n)); 
     } 
     return out.toString(); 
    } 

    @Override 
    protected String doInBackground(Void... params) { 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpContext localContext = new BasicHttpContext(); 
     HttpGet httpGet = new HttpGet("http://192.168.1.156/recess/document/document"); 
     HttpClient client = new DefaultHttpClient(); 
     HttpResponse response=null; 
     try{ 
      response = client.execute(httpGet);} 
     catch(Exception e){} 
     System.out.println(response.getStatusLine()); 
     String text = null; 
     try { 
       response = httpClient.execute(httpGet, localContext); 
       HttpEntity entity = response.getEntity(); 
       text = getASCIIContentFromEntity(entity); 
     } catch (Exception e) { 
      return e.getLocalizedMessage(); 
     } 
     return text; 
    } 

    protected void onPostExecute(String results) { 
     if (results!=null) { 
      EditText et = (EditText)findViewById(R.id.my_edit); 
      et.setText(results); 
     } 
     Button b = (Button)findViewById(R.id.my_button); 
     b.setClickable(true); 
    } 
}} 

答えて

0

あなたはビルトインorg.jsonクラス取得した文字列を変換するには(あなたのtext変数の内容)を使用することができますJSONオブジェクトに変換します。

これを行う方法については、tutorial from Lars Vogelをご覧ください。

+0

ok私は試してみます – Mohit

+0

jsonとリストビューが使用されている非同期タスクのサンプルコードを教えてください – Mohit

関連する問題