2015-08-21 163 views
-2

我有两个一级的API调用。另一个是为api类获取价值。函数返回值为null。我如何从返回值结果中获得价值。 这里是功能类: -从函数获得空值返回值

Jsondeletenote js = new Jsondeletenote(context); 
     String retn = js.deletsubmitData(); 

     Log.d("calllllllllll render", "helo"+retn); 
     // retn value coming null 

这里是API类

public String deletsubmitData() { 

     try { 

      new Thread(new Runnable() { 
       @Override 
       public void run() { 
        try { 
         Looper.prepare(); 
         Log.d("looper", "-->>>>"); 
         try { 
          isAuthorized = isAuthenticated(); 
         } catch (Exception e) { 
          Log.e("Exception ==> ", e.toString()); 
         } 
         MHandler.post(new Runnable() { 
          @Override 
          public void run() { 
           try { 
            if(isAuthorized){ 
             if(AuthenticationResultJSONObject!=null){ 
              Day1 = AuthenticationResultJSONObject.getString("Day"); 
              String Tomorrow = AuthenticationResultJSONObject.getString("Tomorrow"); 
              String Week = AuthenticationResultJSONObject.getString("Week"); 

             } 
            } 
            else 
            { 
             ///Toast.makeText(context, "Loing unsuccessfull, please try again !", Toast.LENGTH_SHORT).show(); 
            } 
           } 
           catch (Exception e) 
           { 
            e.printStackTrace(); 
            Log.e("Exception 146==> ", e.toString()); 
           } 
           finally 
           { 
            //dialog.cancel(); 
           } 
          } 

         }); 
        } catch (Exception e) { 
         Log.e("Exception 153==> ", e.toString()); 
        } 
       } 
      }).start(); 
     } catch (Exception e) { 
      Log.e("Exception 159==> ", e.toString()); 
     } 
     return Day1; 
    } 

    public boolean isAuthenticated() 
    { 
     isAuthorized = false; 

     final String url="www.abcd.com"; 

     try { 


       AuthenticationResultJSONObject = new JSONObject(doFetchDataFromWebService_Simple_OnlyJSONResponse(url));  

       Log.v("Online", "User json array === "+AuthenticationResultJSONObject); 
    else 
      { 
       MHandler.post(new Runnable() { 

        @Override 
        public void run() { 
         // TODO Auto-generated method stub 
         Toast.makeText(context, "Please check your internet connection and try again.", Toast.LENGTH_SHORT).show(); 
        } 
       }); 
      }*/ 
     } 
     catch(Exception e){ 
      e.getMessage(); 
     } 
     finally{ 
      if(AuthenticationResultJSONObject!=null){ 
       isAuthorized = true; 
      } 
      else 
      { 
       isAuthorized=false; 
      } 
     } 
     return isAuthorized; 
    } 

回答

0

你的代码是所有的地方。我建议你重写它..

长话短说,你在一个不同的线程中做异步工作,这意味着“deletsubmitData”的返回值会在线程可能(并且可能)仍然工作时立即返回null 。

你应该做的是改变方法返回无效并添加一个回调方法参数。在线程的操作结束时调用回调。

编辑:
创建一个接口与其中的方法。然后从该接口传递一个新的实例对象作为调用该方法的参数。

像这样:

接口的创建:实例的

public interface IExampleCallBack 
{ 
    void onFinish(String result); 
} 

创作并传递作为参数:

IExampleCallBack callback = new IExampleCallBack(
{ 
    @Override 
    public void onFinish(String result) 
    { 
     //do whatever you want with result. 
    } 
}; 
js.deletsubmitData(callback); 

在结果接口方法的调用:

public void deletsubmitData(IExampleCallBack callback) 
{ 
    //your code inside the thread... 
    //finish thread operation like so 
    String resultExample = "finished"; 
    if (callback != null) 
    { 
     callback.onFinish(resultExample); 
    } 
} 

请注意,如果回调参数为null,则由于空检查,您将永远不会从线程获取回调。但是最好让应用程序崩溃。

祝你好运。

+0

你能帮我解释一下代码吗?如何应用回调方法? –