试图在我的应用程序中创建一个json类

时间:2013-08-13 08:44:26

标签: android json class

我尝试为我的应用程序构建一个json类,我不知道为什么它不起作用: http://pastebin.com/A9Wr9v0m

我需要帮助!谢谢!

2 个答案:

答案 0 :(得分:1)

您是否尝试在UI线程上使用此类?

如果是这样,那么它不起作用的原因是因为你不能在主ui线程上进行网络操作,他们需要在异步类或单独的线程中完成

答案 1 :(得分:0)

使用以下代码: -

  public static String getJsonData(String webServiceName,String parameter)
{  
    try  
    {

    String urlFinal=SERVICE_URI+"/"+webServiceName+"?parameter=";
    HttpPost postMethod = new HttpPost(urlFinal.trim()+""+URLEncoder.encode(parameter,"UTF-8"));
    postMethod.setHeader("Accept", "application/json");
    postMethod.setHeader("Content-type", "application/json");

    HttpClient hc = new DefaultHttpClient();

    HttpResponse response = hc.execute(postMethod);
    Log.i("response", ""+response.toString());
    HttpEntity entity = response.getEntity();
    final String responseText = EntityUtils.toString(entity);

    string=responseText;
    Log.i("Output", ""+responseText);
      }
      catch (Exception e) {
        // TODO: handle exception
    }

       return string;
      }