通过传递参数的Android到URL取得数据 [英] Retrieve Data by passing a parameter to url in Android
问题描述
我需要的是一个参数附加到URL检索数据和放大器;我使用JSON解析器。得到一个完美的结果,我没有传递参数。但是不能够想出如何传递参数如String ID为URL来检索数据。
//内部JSON响应调用类 JSONParser jParser =新JSONParser(); JSONArray jArraySearchJob = jParser.getJSONFromUrl(url_Jobsearch); 尝试{ 的for(int i = 0; I< jArraySearchJob.length();我++)
{
JSONObject的jsonElements = jArraySearchJob.getJSONObject(I) 字符串J_p_id = jsonElements.getString(android_J_P_ID); HashMap的<字符串,字符串> hashAmbJobSearch =新的HashMap<字符串,字符串>(); //将每个子节点HashMap的关键 hashAmbJobSearch.put(android_J_P_ID,J_p_id); //添加HashList到ArrayList的 ResultList_JobSearch.add(hashAmbJobSearch);
}
JSON解析器:
公共类JSONParser { 静态InputStream为= NULL; 静态JSONArray jarray = NULL; 静态JSON字符串=; //方法返回JSON 公共JSONArray getJSONFromUrl(字符串URL){ StringBuilder的建设者=新的StringBuilder(); HttpClient的客户端=新DefaultHttpClient(); HttpPost httppost =新HttpPost(URL);
尝试{
HTT presponse响应= client.execute(httppost); 状态行状态行= response.getStatusLine(); INT状态code = statusLine.getStatus code(); 如果(状态code == 200)
{
HttpEntity实体= response.getEntity(); InputStream的内容= entity.getContent(); 读者的BufferedReader =新的BufferedReader(新的InputStreamReader(内容)); 串线; 而((行= reader.readLine())!= NULL)
{
builder.append(线);
}
}
其他
{
Log.e(==>中,无法下载文件);
}
} 赶上(ClientProtocolException E)
{
e.printStackTrace();
} 赶上(IOException异常E)
{
e.printStackTrace();
} //尝试分析字符串到一个JSON对象
尝试
{
jarray =新JSONArray(builder.toString());
} 赶上(JSONException E)
{
Log.e(JSON解析器,错误分析数据+ e.toString());
} //返回JSON字符串
返回jarray;
}
}
通常是用户名和密码被发送作为参数
HTTP://www.sample.url用户名= userNameValue&放大器;密码= passwordvalue
在Android的情况下。
1.对于get方法作为查询参数
字符串的URL = HTTP://www.sample.url用户名= + Uri.en code(用户名)+&放大器;密码=+ Uri.en code? (密码)
HTTPGET GET =新HTTPGET(URL);
2。对于后mehod作为查询字符串postparam(查询参数是GET方法再次同只)3.如果您想发送的postparams使用下面code
ArrayList的<&的NameValuePair GT; projectLoginInfo =新的ArrayList<&的NameValuePair GT;();
projectLoginInfo.add(新BasicNameValuePair(用户名,userNameValue));
projectLoginInfo.add(新BasicNameValuePair(密码,passwordValue));
HttpPost httppost =新HttpPost(HTTP://www.sample.url); 尝试{// EN code登录数据和双手实体的请求。
httppost.setEntity(新UrlEn codedFormEntity(projectLoginInfo));
}
赶上(UnsupportedEncodingException E1)
{
e1.printStackTrace();
Log.e(UnsupportedEncoding,无法连接code一些字符,E1); 返回-1;
}`
你应该在你的JSON解析器类使用低于code
的ArrayList<&的NameValuePair GT; projectLoginInfo =新的ArrayList<&的NameValuePair GT;();
projectLoginInfo.add(新BasicNameValuePair(用户名,userNameValue));
projectLoginInfo.add(新BasicNameValuePair(密码,passwordValue));
HttpPost httppost =新HttpPost(HTTP://www.sample.url); 尝试{// EN code登录数据和双手实体的请求。
httppost.setEntity(新UrlEn codedFormEntity(projectLoginInfo));
}
赶上(UnsupportedEncodingException E1)
{
e1.printStackTrace();
Log.e(UnsupportedEncoding,无法连接code一些字符,E1); 返回null;
}`
HTT presponse响应= client.execute(httppost);
状态行状态行= response.getStatusLine();
What I need is to append a parameter to a url to retrieve data & I am using json parser. Getting a perfect result where I don't have to pass a parameter. But not being able to figure out how to pass parameter like String ID to url to retrieve data.
//Inside JSON Response calling class
JSONParser jParser = new JSONParser();
JSONArray jArraySearchJob = jParser.getJSONFromUrl(url_Jobsearch);
try{
for (int i = 0; i < jArraySearchJob.length(); i++)
{
JSONObject jsonElements = jArraySearchJob.getJSONObject(i);
String J_p_id = jsonElements.getString(android_J_P_ID);
HashMap<String, String> hashAmbJobSearch = new HashMap<String, String>();
// adding each child node to HashMap key
hashAmbJobSearch.put(android_J_P_ID, J_p_id);
// adding HashList to ArrayList
ResultList_JobSearch.add(hashAmbJobSearch);
}
Json Parser:
public class JSONParser {
static InputStream is = null;
static JSONArray jarray = null;
static String json = "";
//Method Returns JSON
public JSONArray getJSONFromUrl(String url) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
HttpResponse response = client.execute(httppost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200)
{
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null)
{
builder.append(line);
}
}
else
{
Log.e("==>", "Failed to download file");
}
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
// try parse the string to a JSON object
try
{
jarray = new JSONArray(builder.toString());
}
catch (JSONException e)
{
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jarray;
}
}
usually username and password is sent as parameters
http://www.sample.url?Username=userNameValue&Password=passwordvalue
In case of android.
1.For get method as query params
String url = http://www.sample.url?username=+ Uri.encode(UserName) + "&password=" + Uri.encode(password)
HttpGet get = new HttpGet(url);
2. For post mehod as postparam in querystring (query params is again same as get method only)
3. If you want send as postparams use below code
ArrayList<NameValuePair> projectLoginInfo = new ArrayList<NameValuePair>();
projectLoginInfo.add(new BasicNameValuePair("username", userNameValue));
projectLoginInfo.add(new BasicNameValuePair("password", passwordValue));
HttpPost httppost = new HttpPost("http://www.sample.url");
try{ //encode login data and Hands the entity to the request.
httppost.setEntity(new UrlEncodedFormEntity(projectLoginInfo));
}
catch (UnsupportedEncodingException e1)
{
e1.printStackTrace();
Log.e("UnsupportedEncoding", "unable to encode some characters", e1);
return -1;
}`
you should use below code in your Json Parser class
ArrayList<NameValuePair> projectLoginInfo = new ArrayList<NameValuePair>();
projectLoginInfo.add(new BasicNameValuePair("username", userNameValue));
projectLoginInfo.add(new BasicNameValuePair("password", passwordValue));
HttpPost httppost = new HttpPost("http://www.sample.url");
try{ //encode login data and Hands the entity to the request.
httppost.setEntity(new UrlEncodedFormEntity(projectLoginInfo));
}
catch (UnsupportedEncodingException e1)
{
e1.printStackTrace();
Log.e("UnsupportedEncoding", "unable to encode some characters", e1);
return null;
}`
HttpResponse response = client.execute(httppost);
StatusLine statusLine = response.getStatusLine();
这篇关于通过传递参数的Android到URL取得数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!