2011-12-05 80 views
0

在我的应用程序中,我使用webservices.But获取数据,但是当服务器关闭或未处于活动状态时,它会给出响应代码500并关闭我的应用程序。在Android中处理服务器响应

请指导我如何处理我的application.The logcat的输出这一条件如下所示:

12-05 12:17:22.337: V/response code(950): 500 
12-05 12:17:22.347: W/System.err(950): org.json.JSONException: A JSONArray text must start with '[' at character 1 of <html> 

12-05 12:17:22.347: W/System.err(950):  <head> 

12-05 12:17:22.347: W/System.err(950):   <title>Configuration Error</title> 

12-05 12:17:22.347: W/System.err(950):   <style> 

12-05 12:17:22.347: W/System.err(950):   body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} 

12-05 12:17:22.347: W/System.err(950):   p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px} 

12-05 12:17:22.347: W/System.err(950):   b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px} 

12-05 12:17:22.347: W/System.err(950):   H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red } 

12-05 12:17:22.347: W/System.err(950):   H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon } 

12-05 12:17:22.356: W/System.err(950):   pre {font-family:"Lucida Console";font-size: .9em} 

12-05 12:17:22.356: W/System.err(950):   .marker {font-weight: bold; color: black;text-decoration: none;} 

12-05 12:17:22.356: W/System.err(950):   .version {color: gray;} 

12-05 12:17:22.356: W/System.err(950):   .error {margin-bottom: 10px;} 

12-05 12:17:22.356: W/System.err(950):   .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; } 

12-05 12:17:22.356: W/System.err(950):   </style> 

12-05 12:17:22.356: W/System.err(950):  </head> 

12-05 12:17:22.356: W/System.err(950): 

12-05 12:17:22.356: W/System.err(950):  <body bgcolor="white"> 

12-05 12:17:22.366: W/System.err(950): 

12-05 12:17:22.366: W/System.err(950):    <span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1> 

12-05 12:17:22.366: W/System.err(950): 

12-05 12:17:22.366: W/System.err(950):    <h2> <i>Configuration Error</i> </h2></span> 

12-05 12:17:22.366: W/System.err(950): 

12-05 12:17:22.366: W/System.err(950):    <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif "> 

12-05 12:17:22.366: W/System.err(950): 

12-05 12:17:22.366: W/System.err(950):    <b> Description: </b>An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. 

12-05 12:17:22.377: W/System.err(950):    <br><br> 

12-05 12:17:22.377: W/System.err(950): 

12-05 12:17:22.377: W/System.err(950):    <b> Parser Error Message: </b>Unrecognized attribute 'targetFramework'. Note that attribute names are case-sensitive.<br><br> 

12-05 12:17:22.377: W/System.err(950): 

12-05 12:17:22.377: W/System.err(950):    <b>Source Error:</b> <br><br> 

12-05 12:17:22.377: W/System.err(950): 

12-05 12:17:22.377: W/System.err(950):    <table width=100% bgcolor="#ffffcc"> 

12-05 12:17:22.377: W/System.err(950):    <tr> 

12-05 12:17:22.377: W/System.err(950):     <td> 

12-05 12:17:22.377: W/System.err(950):      <code><pre> 

12-05 12:17:22.377: W/System.err(950): 

12-05 12:17:22.377: W/System.err(950): Line 30:    &lt;error statusCode=&quot;404&quot; redirect=&quot;filenotfound.htm&quot; /&gt; 

12-05 12:17:22.377: W/System.err(950): Line 31:   &lt;/customErrors&gt; 

12-05 12:17:22.377: W/System.err(950): <font color=red>Line 32:   &lt;compilation debug=&quot;true&quot; targetFramework=&quot;4.0&quot;&gt; 

12-05 12:17:22.396: W/System.err(950): </font>Line 33:   &lt;expressionBuilders&gt; 

12-05 12:17:22.396: W/System.err(950): Line 34:     &lt;add expressionPrefix=&quot;NopResources&quot; type=&quot;NopSolutions.NopCommerce.BusinessLogic.Localization.NopResourceExpressionBuilder, Nop.BusinessLogic&quot; /&gt;</pre></code> 

12-05 12:17:22.396: W/System.err(950): 

12-05 12:17:22.396: W/System.err(950):     </td> 

12-05 12:17:22.396: W/System.err(950):    </tr> 

12-05 12:17:22.396: W/System.err(950):    </table> 

12-05 12:17:22.396: W/System.err(950): 

12-05 12:17:22.396: W/System.err(950):    <br> 

12-05 12:17:22.396: W/System.err(950): 

12-05 12:17:22.396: W/System.err(950):    <b> Source File: </b> C:\Websites\balajeebazaar\web.config<b> &nbsp;&nbsp; Line: </b> 32 

12-05 12:17:22.396: W/System.err(950):    <br><br> 

12-05 12:17:22.396: W/System.err(950): 

12-05 12:17:22.396: W/System.err(950):    <hr width=100% size=1 color=silver> 

12-05 12:17:22.407: W/System.err(950): 

12-05 12:17:22.407: W/System.err(950):    <b>Version Information:</b>&nbsp;Microsoft .NET Framework Version:2.0.50727.4963; ASP.NET Version:2.0.50727.4955 

12-05 12:17:22.407: W/System.err(950): 

12-05 12:17:22.407: W/System.err(950):    </font> 

12-05 12:17:22.407: W/System.err(950): 

12-05 12:17:22.416: W/System.err(950):  </body> 

12-05 12:17:22.416: W/System.err(950): </html> 

12-05 12:17:22.416: W/System.err(950): <!-- 

12-05 12:17:22.416: W/System.err(950): [ConfigurationErrorsException]: Unrecognized attribute 'targetFramework'. Note that attribute names are case-sensitive. (C:\Websites\balajeebazaar\web.config line 32) 

12-05 12:17:22.416: W/System.err(950): at System.Web.HttpRuntime.HostingInit(HostingEnvironmentFlags hostingFlags) 

12-05 12:17:22.416: W/System.err(950): [HttpException]: Unrecognized attribute 'targetFramework'. Note that attribute names are case-sensitive. (C:\Websites\balajeebazaar\web.config line 32) 

12-05 12:17:22.416: W/System.err(950): at System.Web.HttpRuntime.FirstRequestInit(HttpContext context) 

12-05 12:17:22.416: W/System.err(950): at System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) 

12-05 12:17:22.416: W/System.err(950): at System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) 

12-05 12:17:22.416: W/System.err(950): --><!-- 

12-05 12:17:22.426: W/System.err(950): This error page might contain sensitive information because ASP.NET is configured to show verbose error messages using &lt;customErrors mode="Off"/&gt;. Consider using &lt;customErrors mode="On"/&gt; or &lt;customErrors mode="RemoteOnly"/&gt; in production environments.--> 
12-05 12:17:22.426: W/System.err(950): at org.json.JSONTokener.syntaxError(JSONTokener.java:448) 
12-05 12:17:22.426: W/System.err(950): at org.json.JSONArray.<init>(JSONArray.java:104) 
12-05 12:17:22.426: W/System.err(950): at org.json.JSONArray.<init>(JSONArray.java:150) 
12-05 12:17:22.426: W/System.err(950): at cm.bzaar.CallWebservices.getCategory(CallWebservices.java:48) 
12-05 12:17:22.426: W/System.err(950): at cm.bzaar.CategoryActivity$Loader.doInBackground(CategoryActivity.java:98) 
12-05 12:17:22.426: W/System.err(950): at cm.bzaar.CategoryActivity$Loader.doInBackground(CategoryActivity.java:1) 
12-05 12:17:22.436: W/System.err(950): at android.os.AsyncTask$2.call(AsyncTask.java:185) 
12-05 12:17:22.436: W/System.err(950): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:256) 
12-05 12:17:22.436: W/System.err(950): at java.util.concurrent.FutureTask.run(FutureTask.java:122) 
12-05 12:17:22.436: W/System.err(950): at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:648) 
12-05 12:17:22.436: W/System.err(950): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:673) 
12-05 12:17:22.436: W/System.err(950): at java.lang.Thread.run(Thread.java:1060) 
12-05 12:17:22.436: W/dalvikvm(950): threadid=17: thread exiting with uncaught exception (group=0x4001aa28) 
12-05 12:17:22.446: E/AndroidRuntime(950): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception 
12-05 12:17:22.446: E/AndroidRuntime(950): java.lang.RuntimeException: An error occured while executing doInBackground() 
12-05 12:17:22.446: E/AndroidRuntime(950): at android.os.AsyncTask$3.done(AsyncTask.java:200) 
12-05 12:17:22.446: E/AndroidRuntime(950): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:234) 
12-05 12:17:22.446: E/AndroidRuntime(950): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:258) 
12-05 12:17:22.446: E/AndroidRuntime(950): at java.util.concurrent.FutureTask.run(FutureTask.java:122) 
12-05 12:17:22.446: E/AndroidRuntime(950): at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:648) 
12-05 12:17:22.446: E/AndroidRuntime(950): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:673) 
12-05 12:17:22.446: E/AndroidRuntime(950): at java.lang.Thread.run(Thread.java:1060) 
12-05 12:17:22.446: E/AndroidRuntime(950): Caused by: java.lang.NullPointerException 
12-05 12:17:22.446: E/AndroidRuntime(950): at cm.bzaar.CategoryActivity$Loader.doInBackground(CategoryActivity.java:100) 
12-05 12:17:22.446: E/AndroidRuntime(950): at cm.bzaar.CategoryActivity$Loader.doInBackground(CategoryActivity.java:1) 
12-05 12:17:22.446: E/AndroidRuntime(950): at android.os.AsyncTask$2.call(AsyncTask.java:185) 
12-05 12:17:22.446: E/AndroidRuntime(950): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:256) 
12-05 12:17:22.446: E/AndroidRuntime(950): ... 4 more 

见下CODE:

代码段,其中函数被调用来调用Web服务:

@Override 
     protected Void doInBackground(Void... arg0) { 
      CategoryArray = new JSONArray(); 
      CategoryArray = CW 
        .getCategory("url"); 

      for (int i = 0; i <= CategoryArray.length() - 1; i++) { 
       try { 
        Log.v("category array : ", CategoryArray.getString(i)); 
        String[] val = CategoryArray.getString(i).split("•"); 
        CategoryID.add(i, val[0]); 
        CategoryList.add(i, val[1]); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
      adapter = new CategoryListAdapter(CategoryActivity.this, 
        CategoryList); 
      return null; 
     } 

函数来调用Web服务:

public JSONArray getCategory(String URL){ 

try { 
      HttpPost request = new HttpPost(URL); 
      request.setHeader("Accept", "application/json"); 
      request.setHeader("Content-type", "application/json"); 
      // Send request to WCF service 
      DefaultHttpClient httpClient1 = new DefaultHttpClient(); 
      HttpResponse response = httpClient1.execute(request); 
      Log.v("response code", response.getStatusLine().getStatusCode() 
        + ""); 
      responseCode = response.getStatusLine().getStatusCode(); 
//   if(responseCode > 200){     // HERE I WS TRYING TO BREAK THE CODE AND TRYING TO RETURN NULL BT UNABLE TO DO SO 
//    CategoryArray = new JSONArray(); 
//    CategoryArray.put(false); 
//    return null; 
//   }else{ 
      HttpEntity responseEntity = response.getEntity(); 
      // Read response data into buffer 
      char[] buffer = new char[(int) responseEntity.getContentLength()]; 
      InputStream stream = responseEntity.getContent(); 
      InputStreamReader reader = new InputStreamReader(stream); 
      reader.read(buffer); 
      stream.close(); 
      CategoryArray = new JSONArray(new String(buffer)); 
      Log.v("results length : ", CategoryArray.length() + ""); 
//   } 
     } catch (Exception e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
     } 
     return CategoryArray; 
} 

看到我的反应比较指导code.Please我我如何能打破我的代码,而显示在UI

+1

其Web服务是什么呢?它是SOAP,REST/WCF吗?也尝试发布你的Android代码。 –

+0

它是休息/ WCF的web服务 – Shruti

回答

1

看到您发布的logcat的,我觉得你是取一个JSON字符串作为响应。 当你得到你想要的回应时,你需要解析它。

所以,为了你是不是在你的代码举起之后,我帮助你继续 你给予我的下面的例子中得到你的回应后:

在我的项目响应我的一个从网络服务得到:

{“checkrecord”:[{“rollno”:“abc2”,“percentage”:40,“attend”:12,“missed”:34}],“Table1 “:[]}

为了解析我做了以下操作:

 JSONObject jsonobject = new JSONObject(result); 
     JSONArray array = jsonobject.getJSONArray("checkrecord"); 
     int max = array.length(); 
     for (int j = 0; j < max; j++) 
    { 
     JSONObject obj = array.getJSONObject(j); 
     JSONArray names = obj.names(); 

    for (int k = 0; k < names.length(); k++) 
    { 
     String name = names.getString(k); 
     String value= obj.getString(name); 

    } 

我的JSONObject是这样的:

{ “表1”:[], “checkrecord”:[{ “错过”:34, “出席”:12, “百分比”:40 ,“rollno”:“abc2”}]}

这就是@gwa试图建议的内容。我只是给你一个代码示例。先获得结果并确定它是否有效。

希望它可以帮助

干杯

+0

是的我同意。如果服务器启动并运行良好,我相信她会很快得到200的响应:-)。然后,她“可能”在解析中遇到问题。我不希望发生这种情况,所以我试图帮助告诉她她需要做什么,因为我之前做过类似的事情:-) –

3

UA敬酒同时行你应检查响应代码,通过使用此代码,您可以轻松找出请求是由于服务器关闭还是由于其他原因而成功或失败。

您可以通过检查响应状态代码:

response.getStatusLine().getStatusCode(); 
+0

是我用了那段代码,我得到500 – Shruti

+2

然后享受,你的服务器已关闭:D - :) –

3

的问题是,您使用的是空的或不表示一个JSON字符串的响应创建一个JSON对象。

所以,你应该创建一个基于你的响应JSON对象,如果:

- the status code is 200 
- the response is not empty 
- the response starts with '['