0

我只是通过异步任务构建了一个演示应用程序,现在我想要列表视图中的 json 数据,所以我不知道在哪里可以添加 json 函数和数组列表等,所以请指导我或通过编辑我提前感谢的代码来帮助我,请帮助我刚接触 android 和 java

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);
    }
}}
4

1 回答 1

0

您可以使用内置org.json类将检索到的字符串(您的text变量内容)转换为 JSON 对象。

查看Lars Vogel 的教程,了解如何做到这一点。

于 2012-05-14T08:52:59.263 回答