保存数据错误HttpPost机器人 [英] Save data error HttpPost android
问题描述
之前,我(我使用XAMPP)在我的phpmyadmin有mysql数据库脱机。我可以从我的Android设备或模拟器后(插入)请求。网址是简单的,如本地主机/ cek_user哦,我使用JSON解析数据到PHP。
before, I have database MySQL offline in my phpmyadmin (i using xampp). I can get and post(insert) request from my android device or emulator. The url is simple, like 'localhost/cek_user' oh, i using json for parsing data to php.
然后我决定让它在网上,现在我必须改变整个URL在我的计划,以的http:/ /kun-bfashion.com/cek_user 问题是出在这里。
and then I decide to make it online, now I must change the whole url in my program to 'http://kun-bfashion.com/cek_user' the problem is in here.
我能得到这样的用户名,密码,多与此code,我只是把它在我的课:
I can get like username, password, and more with this code, I just call it in my class:
public String sendGetRequest(String reqUrl){
HttpClient httpClient;
HttpGet httpGet = new HttpGet(serverUri+"/"+reqUrl);
InputStream is = null;
StringBuilder stringBuilder = new StringBuilder();
try {
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 3000);
HttpConnectionParams.setSoTimeout(params, 3000);
httpClient = new DefaultHttpClient(params);
Log.d(TAG, "executing...");
HttpResponse httpResponse = httpClient.execute(httpGet);
StatusLine status = httpResponse.getStatusLine();
if(status.getStatusCode() == HttpStatus.SC_OK && httpResponse != null){
/* mengambil response string dari server */
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = null;
while((line = reader.readLine()) != null){
stringBuilder.append(line+"\n");
}
is.close();
}
} catch (Exception e) {
//Log.d(TAG, e.getMessage());
}
return stringBuilder.toString();
}
serverUri和reqUrl是我的地盘,我只是将它设置为静态 http://kun-bfashion.com 和reqURL就像是'cek_user。我用code进行登录,从服务器等等。
serverUri and reqUrl is my site, I just setting it to static 'http://kun-bfashion.com, and reqURL is like 'cek_user'. i use that code for login, get data from server and many more.
但是当我要张贴像注册用户,其表现土司保存数据的问题,我用这个code为发布到网络服务
but when i want to post like registration user, its showing toast "save data problem", i use this code for post to web service
public int sendPostRequest(User user, String url){
int replyCode = 99;
HttpClient httpClient;
HttpPost post = new HttpPost(this.serverUri+"/"+url);
List<NameValuePair> value = new ArrayList<NameValuePair>();
/* menambahkan parameter ke dalam request */
value.add(new BasicNameValuePair("id_user", user.getId_User().toString()));
value.add(new BasicNameValuePair("username", user.getUsername()));
value.add(new BasicNameValuePair("nama_user", user.getNamaUser()));
value.add(new BasicNameValuePair("email_user", user.getEmailUser()));
value.add(new BasicNameValuePair("password_user",user.getPasswordUser()));
try {
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 3000);
HttpConnectionParams.setSoTimeout(params, 3000);
httpClient = new DefaultHttpClient(params);
post.setEntity(new UrlEncodedFormEntity(value));
Log.d(TAG, "executing post...");
HttpResponse httpResponse = httpClient.execute(post);
StatusLine status = httpResponse.getStatusLine();
if(status.getStatusCode() == HttpStatus.SC_OK){
Log.d(TAG, "submitted sucessfully...");
replyCode = status.getStatusCode();
}
} catch (IOException e) {
Log.d(TAG, e.getMessage());
}
return replyCode;
}
该错误是显示吐司保存数据问题,但在我的离线数据库(本地主机)之前,我可以插入后,并得到没有任何错误。
The Error is showing Toast "Save Data Problem", but before in my offline database (localhost) i can insert post and get without no error.
这是我的LogCat中,当我preSS registrastion按钮:
this is my LogCat when i press registrastion button:
03-15 08:19:33.032: I/System.out(23214): ex:org.apache.http.NoHttpResponseException: The target server failed to respond
03-15 08:19:33.032: I/System.out(23214): retry3
03-15 08:19:33.033: I/System.out(23214): [socket][13] connection /66.85.138.130:80;LocalPort=53906(3000)
03-15 08:19:33.033: I/System.out(23214): [CDS]connect[/66.85.138.130:80] tm:3
03-15 08:19:33.034: D/Posix(23214): [Posix_connect Debug]Process com.koenb_fashion_fix :80
03-15 08:19:33.056: I/System.out(23214): [socket][/192.168.1.111:53906] connected
03-15 08:19:33.056: I/System.out(23214): [CDS]rx timeout:3000
03-15 08:19:33.056: I/System.out(23214): >doSendRequest
03-15 08:19:33.058: I/System.out(23214): <doSendRequest
03-15 08:19:33.331: I/SurfaceTextureClient(23214): [STC::queueBuffer] (this:0x53ecad00) fps:50.80, dur:1023.54, max:43.97, min:3.79
03-15 08:19:33.505: I/SurfaceTextureClient(23214): [STC::queueBuffer] (this:0x53e75dd0) fps:1.97, dur:1015.42, max:508.04, min:507.38
03-15 08:19:33.523: I/System.out(23214): [CDS]close[53906]
03-15 08:19:33.524: I/System.out(23214): close [socket][/0.0.0.0:53906]
03-15 08:19:33.538: I/System.out(23214): ex:org.apache.http.NoHttpResponseException: The target server failed to respond
03-15 08:19:33.539: I/System.out(23214): retry4
03-15 08:19:33.539: I/System.out(23214): close [socket][/0.0.0.0:53906]
03-15 08:19:33.540: I/System.out(23214): close [socket][/0.0.0.0:53906]
03-15 08:19:33.540: D/ServerRequest(23214): The target server failed to respond
对不起长的帖子,但我不知道怎么解释我的问题-_-。感谢您的阅读。
sorry for long post, but i don't know how to explain my problem -_-. thanks for read.
问题是的我可以从服务器获取,但我不能发布
推荐答案
这可能是服务器的问题,但根据<一个href=\"http://stackoverflow.com/questions/24970431/android-the-target-server-failed-to-respond/25009763#25009763\">This帖子这个问题可能通过更改code到的HttpParams适用于HttpPost而不是DefaultHttpClient解决:
It could be a server issue, but according to This post the issue might be solved by changing your code to apply the HttpParams to the HttpPost instead of the DefaultHttpClient:
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 3000);
HttpConnectionParams.setSoTimeout(params, 3000);
//httpClient = new DefaultHttpClient(params);
httpClient = new DefaultHttpClient(); //use default constructor instead
post.setParams(params); //add call to setParams on HttpPost
post.setEntity(new UrlEncodedFormEntity(value));
编辑:
日志记录验证网址:
Validate the url with logging:
HttpPost post = new HttpPost(this.serverUri+"/"+url);
Log.d(TAG, "post url: " + this.serverUri+"/"+url );
测试网址在浏览器中,看看你得到任何JSON响应。
Test the url in a browser and see if you get any json response.
这篇关于保存数据错误HttpPost机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!