我正在构建一个Android应用程序,用户输入的值就像 name和emailId,这个值被解析为一个URL,作为回应我得到 用户ID是该用户的主键。
我以成功消息的数组形式获取ID。喜欢
这个 - {"message" : "succefull", "userID":"34341390ssvjsv"}
。
现在我需要将此userID保存在此数组的字符串中。
如何存储此ID" 34341390ssvjsv"在一个字符串?
这是代码 -
private class DownloadJSON extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
}
@Override
protected String doInBackground(String... params) {
InputStream inputStream = null;
String result = "";
// Create an array
// public String postData() {
HttpClient httpclient = new DefaultHttpClient();
String URl = "http://url/api/user";
HttpPost httppost = new HttpPost(URl);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
3);
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("email", email));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
inputStream = response.getEntity().getContent();
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}
private String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader( new
InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), result,
Toast.LENGTH_LONG).show();
}
}
在上面这个关于吐司结果的代码中,我得到{&#34; message&#34; :&#34;成功&#34;,&#34;用户ID&#34;:&#34; 34341390ssvjsv&#34;}
答案 0 :(得分:0)
您可以使用此代码 - String re = result.substring(37,3);