2016-07-29 63 views
1

我有关于访问REST API的代码的正确语法和顺序的问题。使用请求标头在Android中访问REST API

我想访问一个我在mBaas上调用backendless.com的数据库。 (以下数据信息是特定于此mBaas,但我的问题是更多关于在Android中访问REST API的一般过程)

根据他们的批量删除教程(https://backendless.com/documentation/data/rest/data_deleting_data_objects.htm),我需要一个URL来查询我的数据库具体的值,然后删除它。我有这个价值。他们还需要3个请求头(应用程序ID,密钥,应用程序类型)。我也有这些。

我在一个ASyncTask类中使用了所有这些信息,这些信息在技术上应该打开url,设置请求标头,并调用REST API。我唯一的问题是,我不知道我在这里是否缺少某种代码?我的当前代码是否正确?每当我的课程被执行时,什么都不会发生。

我还得到关于我的URL日志猫异常:java.io.FileNotFoundException:api.backendless.com/v1/data/bulk/...

的URL不会导致任何事情的时候我把它放在我的浏览器中,但我被告知它不应该被浏览器发送为GET请求。

无论如何,这里是我的ASyncTask类与所有的信息。有谁知道这段代码看起来是否正确,或者我在这里错过了什么?我不熟悉进行这种类型的调用,并且不太了解request-header在访问REST API时所发挥的作用。请告诉我。谢谢!

class DeleteBulkFromBackEnd extends AsyncTask<Void,Void,String>{ 
    final String API_URL = "https://api.backendless.com/v1/data/bulk/LocalPhoneNum?where%3DuserEmailID%[email protected]"; 

    @Override 
    protected String doInBackground(Void... params) { 
     HttpURLConnection urlConnection = null; 


     try { 
      URL url = new URL(API_URL); 
      urlConnection = (HttpURLConnection)url.openConnection(); 

     urlConnection.setRequestProperty("application-id","12345678"); 
      urlConnection.setRequestProperty("secret-key","12345678"); 
      urlConnection.setRequestProperty("application-type", "REST"); 
      urlConnection.connect(); 



     } catch (MalformedURLException e) { 

      e.printStackTrace(); 
     } catch (IOException e) { 
      Log.d("Contact","ERROR " + e.toString());//IO Exception Prints in log cat not recognizing URL 
      e.printStackTrace(); 
     }finally { 
      urlConnection.disconnect(); 
     } 

     return null; 
    } 

} 
+1

考虑到'API_URL'和头部参数是正确的,你可能需要在调用'urlConnection.connect()之前调用'urlConnection.setRequestMethod(“DELETE”);'' –

+0

是的,这是答案!请发布,以便我可以接受。谢谢! –

回答

1

正如我评论,这里的解决方案:

class DeleteBulkFromBackEnd extends AsyncTask<Void,Void,String>{ 
    final String API_URL = "https://api.backendless.com/v1/data/bulk/LocalPhoneNum?where%3DuserEmailID%[email protected]"; 

    @Override 
    protected String doInBackground(Void... params) { 
     HttpURLConnection urlConnection = null; 


     try { 
      URL url = new URL(API_URL); 
      urlConnection = (HttpURLConnection)url.openConnection(); 

      urlConnection.setRequestProperty("application-id","12345678"); 
      urlConnection.setRequestProperty("secret-key","12345678"); 
      urlConnection.setRequestProperty("application-type", "REST"); 
      urlConnection.setRequestMethod("DELETE"); 

      urlConnection.connect(); 



     } catch (MalformedURLException e) { 

      e.printStackTrace(); 
     } catch (IOException e) { 
      Log.d("Contact","ERROR " + e.toString());//IO Exception Prints in log cat not recognizing URL 
      e.printStackTrace(); 
     }finally { 
      urlConnection.disconnect(); 
     } 

     return null; 
    } 

} 
2

我建议你使用okhttp,方便的网络接入。

并检查响应代码和响应正文。

在你的build.gradle:

compile 'com.squareup.okhttp3:okhttp:3.4.1' 

你的AsyncTask将是这样的:

class DeleteBulkFromBackEnd extends AsyncTask<Void, Void, String> { 

    final String API_URL = "https://api.backendless.com/v1/data/bulk/LocalPhoneNum?where%3DuserEmailID%[email protected]"; 
    final OkHttpClient mClient; 

    public DeleteBulkFromBackEnd(OkHttpClient client) { 
     mClient = client; 
    } 

    @Override 
    protected String doInBackground(Void... params) { 
     try { 
      Request request = new Request.Builder() 
        .url(API_URL) 
        .delete() 
        .header("application-id", "12345678") 
        .header("secret-key", "12345678") 
        .header("application-type", "REST") 
        .build(); 

      Response response = mClient.newCall(request).execute(); 

      Log.d("DeleteBulkFromBackEnd", "Code: " + response.code()); 
      Log.d("DeleteBulkFromBackEnd", "Body: " + response.body().string()); 

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

     return null; 
    } 
} 

执行的AsyncTask是这样的:

OkHttpClient client = new OkHttpClient(); 

void someMethod() { 
    ... 
    new DeleteBulkFromBackEnd(client).execute(); 
    ... 
} 
相关问题