Android 如何将安卓应用程序中的数据发送到html并接收信息?
我需要发送三条信息: 域、电子邮件和移动标识符 从网站上,我希望得到如下的JSON响应:Android 如何将安卓应用程序中的数据发送到html并接收信息?,android,webview,http-post,http-get,Android,Webview,Http Post,Http Get,我需要发送三条信息: 域、电子邮件和移动标识符 从网站上,我希望得到如下的JSON响应: { domain: example.com, token: abcdefg123, reportEmail: null, mobileResultsPageUrl: http://completedtest.testingSite.com/mobile/abcdefg123/4a0ced51c00cfd5d6ce8b50d00be49f6 } 我最关心的是在新活动中显示为Webview的Url 这就是我目
{
domain: example.com,
token: abcdefg123,
reportEmail: null,
mobileResultsPageUrl: http://completedtest.testingSite.com/mobile/abcdefg123/4a0ced51c00cfd5d6ce8b50d00be49f6
}
我最关心的是在新活动中显示为Webview的Url
这就是我目前正在尝试的:
public void postData(){
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://exampleTestingSite.com/");
try {
// Add your data
List<NameValuePair> pairs = new ArrayList<NameValuePair>(3);
pairs.add(new BasicNameValuePair("domain", "OswaldMagic.com"));
pairs.add(new BasicNameValuePair("sendReportTo", "me@gmail.com"));
pairs.add(new BasicNameValuePair("mobile", "1"));
httppost.setEntity(new UrlEncodedFormEntity(pairs));
// 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
}
}
public void postData(){
//创建一个新的HttpClient和Post头
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://exampleTestingSite.com/");
试一试{
//添加您的数据
列表对=新的ArrayList(3);
添加(新的BasicNameValuePair(“domain”,“OswaldMagic.com”);
添加(新的BasicNameValuePair(“sendReportTo”)me@gmail.com"));
添加(新的BasicNameValuePair(“移动”、“1”));
setEntity(新的UrlEncodedFormEntity(对));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
}
任何帮助都将不胜感激。保重,快点说,
-Stefan现在开始工作了
我们必须让网站知道,我们希望返回一个JSON,因为默认设置为HTML。我们通过修改标头以接受JSON来实现这一点,请参见代码:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setHeader("Accept", "application/json");
在得到预期结果之后,我只需要使用JSON的一部分。首先,我从站点获取结果并将其转换为字符串:
resultHTML = Html.fromHtml(responseBody);
JSONObject obj = null;
try {
obj = new JSONObject(String.valueOf(resultHTML));
} catch (JSONException e) {
e.printStackTrace();
}
String resultsPageHTML = null;
try {
resultsPageHTML = obj.getString("mobileResultsPageUrl");
} catch (JSONException e) {
e.printStackTrace();
}
然后我打电话给SharedReferences:
// create a file to save SharedPreferences in
public static final String PREFS_NAME = "MyPreferencesFile";
// call sharedPreferences
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
// call SharedPreferences editor
SharedPreferences.Editor editor = prefs.edit();
editor.putString("resultsPageHTML", resultsPageHTML);
editor.commit();
这样我就可以在另一个活动中收到它
我希望这能帮助正在经历类似困难的人。您不能将数据发布到html。你试过了。你走了多远?请做一份详细的报告。别让我们猜,我一点也没走远。你是说我们只能从网站获取数据吗?我们有没有办法使用网站作为处理数据的工具?你想要的一切都是可以做到的。有许多标准方法。你不想要什么特别的东西。