Привет, я общаюсь от приложения к odesk. Я получил сообщения и отобразился в своем приложении, но если я отправлю вместо space
, это места %20
. Вот мой кодКак отправить и декодировать строку в спокойной веб-службе?
reply = (EditText)findViewById(R.id.reply);
Здесь я получу текст из моего edittext и преобразован в строку.
class ODESksendReplyTask extends AsyncTask<Void, Void, String>
{
public ProgressDialog _dialog;
@Override
protected void onPreExecute()
{
_dialog = new ProgressDialog(context);
_dialog.setMessage("Sending Reply...");
_dialog.setIndeterminate(true);
_dialog.setCancelable(false);
_dialog.show();
super.onPreExecute();
}
@Override
protected String doInBackground(Void... params)
{
client.setTokenWithSecret(token,tokenSecret);
Mc mc = new Mc(client);
HashMap<String, String> params2 = new HashMap<String, String>();
params2.put("body", replyMsg);
try {
mc.replyToThread(myId,id,params2);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated method stub
return null;
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
if (_dialog.isShowing())
{
_dialog.dismiss();
Toast.makeText(MessageThreadReplyActivity.this, "Successfully sent",Toast.LENGTH_LONG).show();
}
}
}
Это replyToThread метод:
public JSONObject replyToThread(String username, String threadId, HashMap<String, String> params) throws JSONException {
return oClient.post("/mc/v1/threads/" + username + "/" + threadId, params);
}
Вот пост метод:
public JSONObject post(String url, HashMap<String, String> params) throws JSONException {
return sendPostRequest(url, METHOD_POST, params);
}
Вот sendPostRequest метод
private JSONObject sendPostRequest(String url, Integer type, HashMap<String, String> params) throws JSONException {
String fullUrl = getFullUrl(url);
HttpPost request = new HttpPost(fullUrl);
switch(type) {
case METHOD_PUT:
case METHOD_DELETE:
// assign overload value
String oValue;
if (type == METHOD_PUT) {
oValue = "put";
} else {
oValue = "delete";
}
params.put(OVERLOAD_PARAM, oValue);
case METHOD_POST:
break;
default:
throw new RuntimeException("Wrong http method requested");
}
// add parameters to request
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(params.size());
for (Map.Entry<String, String> entry : params.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
nameValuePairs.add(new BasicNameValuePair(key, OAuth.percentEncode(value)));
//nameValuePairs.add(new BasicNameValuePair(key,value));
}
try {
request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// sign request
try {
mOAuthConsumer.sign(request);
}
catch (OAuthException e) {
e.printStackTrace();
}
return oDeskRestClient.getJSONObject(request, type, params);
}
Final Митос doPostRequest:
private static JSONObject doPostRequest(HttpPost httpPost, HashMap<String, String> params) throws JSONException {
JSONObject json = null;
//HttpClient postClient = HttpClientBuilder.create().build();
DefaultHttpClient postClient = new DefaultHttpClient();
HttpResponse response;
try {
response = postClient.execute(httpPost);
if(response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String result = convertStreamToString(instream);
instream.close();
json = new JSONObject(result);
}
} else {
json = oDeskRestClient.genError(response);
}
} catch (ClientProtocolException e) {
json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: ClientProtocolException");
} catch (IOException e) {
json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: IOException");
} catch (JSONException e) {
json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: JSONException");
} catch (Exception e) {
json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: Exception " + e.toString());
} finally {
httpPost.abort();
}
return json;
}
Проблема заключается в том, когда я отправить сообщение пример, как
Hello how are you man
Hello%20how%20are%20you%20man
Пожалуйста, помогите мне, я не знаю, где я ошибаюсь.
URL-адреса не могут содержать пробелы. Кодировка URL обычно заменяет пробел знаком плюс (+) или% 20. – Gattsu
Итак, когда декодирование закончено? это сайт или я? – Nava2011
проверить мой ответ – Gattsu