使用asynctask时如何修复oauth问题?

时间:2017-02-17 07:04:24

标签: android json api android-asynctask

祝你有愉快的一天, 我在使用xero api时遇到问题,我把数据放到了服务器的json对象,但我得到的结果是null。我调试它说oauth_problem =消费者密钥未知但我已经检查了我的密钥和密钥,它是真的。但是当我运行应用程序时没有任何事情发生,我需要只将用户信息放到服务器,但我不知道如何解决它。我花了一个星期关于这个,所以我真的需要你的帮助! 谢谢〜 这是我的代码:

    public String PUT_CONTACT(String url, Contact c){
    String oauth = "Basic " + Base64.encodeToString(
            (getString(R.string.api_key) + ":" +        
    getString(R.string.api_secret)).getBytes(),
            Base64.NO_WRAP);
    String kq=null;
    InputStream is;

    HttpClient httpClient = new DefaultHttpClient();

    HttpPut httpPut = new HttpPut(url);

    httpPut.addHeader("Authentication",oauth);

    String js ="";
    JSONObject ob = new JSONObject();
    try {
        ob.accumulate("Name",c.getName());
        ob.accumulate("Email",c.getEmail());
        ob.accumulate("Job",c.getJob());
    } catch (JSONException e) {
        e.printStackTrace();
    }

    js = ob.toString();

    try {
        StringEntity se = new StringEntity(js);

        httpPut.setEntity(se);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    httpPut.addHeader("Accept","application/json");
    httpPut.addHeader("Content-Type","application/json");

    try {
        HttpResponse response = httpClient.execute(httpPut);

        is = response.getEntity().getContent();

        if (is!=null){
            kq = convertInputStreamToString(is);
        }else {
            kq ="";
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return kq;
}
private static String convertInputStreamToString (InputStream is) throws IOException{
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    String line ="";
    String result ="";
    while ((line=reader.readLine())!=null){
        result+=line;
        is.close();
    }
    return  result;
}
class putContactToServer extends AsyncTask<String,Void,String>{

    @Override
    protected String doInBackground(String... url) {

        return PUT_CONTACT(url[0],c);
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Toast.makeText(ResponseActivity.this, "putted"+s, Toast.LENGTH_SHORT).show();
        rs.setText(s);
    }
}

0 个答案:

没有答案