我有一个网址“http://184.82.158.234/~store/rest/system/connect.json”和发布此网址与mozilla插件称为海报返回数据的形式json 我想要的是从Android发布此网址以将该json数据转换为android视图。从Android发布URL来检索数据
任何帮助,高度赞赏 感谢
我有一个网址“http://184.82.158.234/~store/rest/system/connect.json”和发布此网址与mozilla插件称为海报返回数据的形式json 我想要的是从Android发布此网址以将该json数据转换为android视图。从Android发布URL来检索数据
任何帮助,高度赞赏 感谢
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://184.82.158.234/~store/rest/system/connect.json");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
响应变量将包含您的JSON数据。
查看下面的代码:试试这可能会帮助你。
ArrayList nameValuePairs1 = new ArrayList();
nameValuePairs1.add(new BasicNameValuePair("user_id", ""));
nameValuePairs1.add(new BasicNameValuePair("product_id", ""));
nameValuePairs1.add(new BasicNameValuePair("product_review",""+text));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs1));
HttpResponse responce = httpclient.execute(httppost);
HttpEntity entity = responce.getEntity();
is = entity.getContent();
BufferedReader bufr = new BufferedReader(new InputStreamReader(is1,"iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
sb.append(bufr.readLine() + "\n");
String line = "0";
while ((line = bufr.readLine()) != null)
{
sb.append(line + "\n");
}
is1.close();
result = sb.toString();
结果是一个json字符串。解析该json并在任何控件中显示。我在文本视图中显示了这一点,见下文。
好吧,让我试试 你可以告诉我这是什么?代码 nameValuePairs1.add(new BasicNameValuePair(“user_id”,“”)); nameValuePairs1.add(new BasicNameValuePair(“product_id”,“”)); nameValuePairs1.add(new BasicNameValuePair(“product_review”,“”+ text));'code' 因为我得到的响应没有任何参数 –
这是你必须发送的参数, www.xyz.com?name=dhaval&product_id=1&product_review=4。这是get方法,但如果你想使用post方法,那么你必须传递数组中的数据,就像我在使用nameValuePairs1数组的答案中写的一样。 –
*** *** 05-14 17:35:50.541:E/AndroidRuntime(1216):java.lang.RuntimeException:无法启动活动ComponentInfo {com.post/com.post.PostActivity}:android.os.NetworkOnMainThreadException * * 这是让我的应用程序崩溃,我不知道如何解决它 帮助plzzz –
这里是一个函数,也许你可以用来发布一个字符串到一个URL。
public String doHttpPost(final String fullUrl, final String body) {
final URL url = new URL(fullUrl);
final HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
// set the request mode as POST
urlConnection.setRequestMethod("POST");
urlConnection.setUseCaches(false);
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Accept-charset", "utf-8");
urlConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
final DataOutputStream request = new DataOutputStream(urlConnection.getOutputStream());
// write the body.
request.writeBytes(body);
// flush output buffer
request.flush();
request.close();
// construct a read using input stream and charset.
final InputStreamReader isr = new InputStreamReader(urlConnection.getInputStream(), CHARSET_UTF8);
final BufferedReader in = new BufferedReader(isr);
String inputLine;
final StringBuilder stringBuilder = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
stringBuilder.append(inputLine).append("\n");
}
in.close();
isr.close();
urlConnection.disconnect();
return stringBuilder.toString();
}
的兄弟,我已经尝试过乌拉圭回合的解决方案不工作的感谢答复 –
和U可以PLZ告诉我Ÿ我不得不这样做 nameValuePairs.add(新BasicNameValuePair(“ID”,“12345”)); 因为我的网址不采取任何参数它只是返回数据没有任何参数 –
你不能解析json吗?你可以扩大你的问题多一点 –