0

`

public class JsonExampleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_json_example);
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://codeincloud.tk/json_android_example.php");
        TextView textView = (TextView)findViewById(R.id.textView1);
        try {

            HttpResponse response = httpclient.execute(httppost);
            String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
            JSONObject object = new JSONObject(jsonResult);

            String name = object.getString("name");
            String verion = object.getString("version");
            textView.setText(name + " - " + verion);

        } 
        catch (JSONException e) {
            e.printStackTrace();
        } 
        catch (ClientProtocolException e) {
            e.printStackTrace();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }


       }
    private StringBuilder inputStreamToString(InputStream is) {
        String rLine = "";
        StringBuilder answer = new StringBuilder();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));

        try {
         while ((rLine = rd.readLine()) != null) 
          answer.append(rLine);
           }
        }

        catch (IOException e) {
            e.printStackTrace();
         }
        return answer;
       }
}`
4

2 回答 2

-1

异常的基础似乎来自“FileOutputStream.write”,您是否将写入权限添加到您的清单?

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
于 2013-05-13T07:21:30.977 回答
-1

当您尝试从无法执行的线程更新视图时,会发生此错误。您要么需要从 onPostExecute 更新您的视图,要么使用 runOnUiThread 来更新您的视图。

您可以按如下方式使用 runOnUiThread:

runOnUiThread(new Runnable() {
    public void run() {                     
        // your code to update the UI thread here               
    }
});
于 2013-05-13T07:35:49.427 回答