Java Android:将字符串上传到服务器
我试图让我的用户能够向我的服务器报告我的android应用程序自动捕获的小错误。没什么大不了的,只是一个小文本框和发送按钮 它应该将3个字符串(错误、可选用户描述和时间)发送到我的网站上专门用来捕获这些错误的文件中。问题是,我完全不知道怎么做。我只知道如何让我的应用程序读取我网站上的信息,但不知道如何让我的应用程序读取我网站上的信息 我必须在我的网站上有一个特殊的脚本吗?JSON字符串是必需的吗?我需要把字符串保存在那里。(不是暂时的)Java Android:将字符串上传到服务器,java,android,web,webserver,httpclient,Java,Android,Web,Webserver,Httpclient,我试图让我的用户能够向我的服务器报告我的android应用程序自动捕获的小错误。没什么大不了的,只是一个小文本框和发送按钮 它应该将3个字符串(错误、可选用户描述和时间)发送到我的网站上专门用来捕获这些错误的文件中。问题是,我完全不知道怎么做。我只知道如何让我的应用程序读取我网站上的信息,但不知道如何让我的应用程序读取我网站上的信息 我必须在我的网站上有一个特殊的脚本吗?JSON字符串是必需的吗?我需要把字符串保存在那里。(不是暂时的) 我是一个新手,所以非常感谢你的帮助。谢谢 您只需通过htt
我是一个新手,所以非常感谢你的帮助。谢谢 您只需通过http将值发布到服务器上的php脚本中,该脚本将这些值保存在您的文件或数据库中 -必须有一个
脚本运行在您的服务器上,例如:php脚本
-它实际上是一个发布在服务器上的web服务,以便客户端可以访问它
-然后您需要对服务器执行httppost
,最好同时使用NameValuePair
发送数据
这是我的HTTP POST代码:
public String postData(String url, String xmlQuery) {
final String urlStr = url;
final String xmlStr = xmlQuery;
final StringBuilder sb = new StringBuilder();
Thread t1 = new Thread(new Runnable() {
public void run() {
HttpClient httpclient = MySSLSocketFactory.getNewHttpClient();
HttpPost httppost = new HttpPost(urlStr);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
1);
nameValuePairs.add(new BasicNameValuePair("xml", xmlStr));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Log.d("Vivek", response.toString());
HttpEntity entity = response.getEntity();
InputStream i = entity.getContent();
Log.d("Vivek", i.toString());
InputStreamReader isr = new InputStreamReader(i);
BufferedReader br = new BufferedReader(isr);
String s = null;
while ((s = br.readLine()) != null) {
Log.d("YumZing", s);
sb.append(s);
}
Log.d("Check Now", sb + "");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} /*
* catch (ParserConfigurationException e) { // TODO
* Auto-generated catch block e.printStackTrace(); } catch
* (SAXException e) { // TODO Auto-generated catch block
* e.printStackTrace(); }
*/
}
});
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Getting from Post Data Method " + sb.toString());
return sb.toString();
}
<?php
require_once(ROOT.'/lab/lib/xyz_api_int.php');
try {
//setup the sdk
$api = YumzingApiInt::_get(
Config::get('api_int','url'),
Config::get('api_int','key'),
Config::get('api_int','secret')
);
//connect to the api
$api->connect();
//check our token
echo $api->getToken();
} catch(Exception $e){
sysError($e->getMessage());
}
publicstringpostdata(stringurl,stringxmlquery){
最后一个字符串urlStr=url;
最终字符串xmlStr=xmlQuery;
最终StringBuilder sb=新StringBuilder();
线程t1=新线程(新的可运行线程(){
公开募捐{
HttpClient-HttpClient=mysslssocketfactory.getNewHttpClient();
HttpPost HttpPost=新的HttpPost(urlStr);
试一试{
List name valuepairs=new ArrayList(
1);
添加(新的BasicNameValuePair(“xml”,xmlStr));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
Log.d(“Vivek”,response.toString());
HttpEntity=response.getEntity();
InputStream i=entity.getContent();
Log.d(“Vivek”,i.toString());
InputStreamReader isr=新的InputStreamReader(i);
BufferedReader br=新的BufferedReader(isr);
字符串s=null;
而((s=br.readLine())!=null){
Log.d(“YumZing”,s);
某人追加;
}
Log.d(“立即检查”,sb+”);
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
} /*
*捕获(ParserConfiguration异常e){//TODO
*自动生成的捕获块e.printStackTrace();}catch
*(SAXE异常){//TODO自动生成的捕捉块
*e.printStackTrace();}
*/
}
});
t1.start();
试一试{
t1.join();
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
System.out.println(“从Post数据方法获取”+sb.toString());
使某人返回字符串();
}
//编辑的部分
服务器端php代码:
public String postData(String url, String xmlQuery) {
final String urlStr = url;
final String xmlStr = xmlQuery;
final StringBuilder sb = new StringBuilder();
Thread t1 = new Thread(new Runnable() {
public void run() {
HttpClient httpclient = MySSLSocketFactory.getNewHttpClient();
HttpPost httppost = new HttpPost(urlStr);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
1);
nameValuePairs.add(new BasicNameValuePair("xml", xmlStr));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Log.d("Vivek", response.toString());
HttpEntity entity = response.getEntity();
InputStream i = entity.getContent();
Log.d("Vivek", i.toString());
InputStreamReader isr = new InputStreamReader(i);
BufferedReader br = new BufferedReader(isr);
String s = null;
while ((s = br.readLine()) != null) {
Log.d("YumZing", s);
sb.append(s);
}
Log.d("Check Now", sb + "");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} /*
* catch (ParserConfigurationException e) { // TODO
* Auto-generated catch block e.printStackTrace(); } catch
* (SAXException e) { // TODO Auto-generated catch block
* e.printStackTrace(); }
*/
}
});
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Getting from Post Data Method " + sb.toString());
return sb.toString();
}
<?php
require_once(ROOT.'/lab/lib/xyz_api_int.php');
try {
//setup the sdk
$api = YumzingApiInt::_get(
Config::get('api_int','url'),
Config::get('api_int','key'),
Config::get('api_int','secret')
);
//connect to the api
$api->connect();
//check our token
echo $api->getToken();
} catch(Exception $e){
sysError($e->getMessage());
}
我该如何做到这一点呢?通过学习一些基本的php并了解如何将数据发送到脚本。你可以通过调用一个url发送它,该url看起来像“(这将是一个get请求,但帖子更适合这份工作)。谢谢!这非常有用!你介意给我看一下服务器上运行的脚本吗?@Pkmmte我已经按照你的要求添加了服务器端脚本…请查看编辑的部分