连接到https://xxx.x.x.x拒绝 [英] Connection to https://xxx.x.x.x refused
问题描述
我试图使用PHP脚本和JSON response.But当我运行程序我得到的输出无法连接到数据库检索存储在MySQL数据库中的数据。
MainActivity.java
公共类MainActivity延伸活动{
TextView的resultView;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
StrictMode.enableDefaults();
resultView =(的TextView)findViewById(R.id.result);
的getData();
}公共无效的getData(){
字符串结果=;
InputStream的ISR = NULL;
尝试{
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(HTTPS://192.168.x.xx/sheetal/getdata.php);
HTT presponse响应= httpclient.execute(httppost);
HttpEntity实体= response.getEntity();
ISR = entity.getContent(); }赶上(例外五){
Log.e(log_tag,在HTTP连接错误+ e.toString());
resultView.setText(Could'nt连接到数据库);
}
//响应转换为字符串
尝试{ 的BufferedReader读者=新的BufferedReader(新的InputStreamReader(ISR,ISO-8859-1),8);
StringBuilder的SB =新的StringBuilder();
串线= NULL;
而((行= reader.readLine())!= NULL){
sb.append(行+\\ n);
}
isr.close();
结果= sb.toString();
}赶上(例外五){
Log.e(log_tag,错误转换结果+ e.toString());
} //解析JSON数据
尝试{
字符串s =;
JSONArray jArray =新JSONArray(结果); 的for(int i = 0; I< jArray.length();我++){
JSONObject的JSON = jArray.getJSONObject(I)
S = S +
姓名:+ json.getString(名字)++ json.getString(姓氏)+\\ n+
年龄:+ json.getInt(时代)+\\ n+
手机使用:+ json.getString(产品)+\\ n \\ n; } resultView.setText(多个);
}赶上(例外五){
Log.e(登录TEG,错误解析数据+ e.toString());
}
}
}
这是我的PHP文件访问getdata.php
< PHP
$ CON = mysql_connect(localhost的根,);如果(!$ CON)
{
死亡(无法连接:'mysql_error());
}mysql_select_db(测试1,$ CON);
$结果= mysql_query(SELECT * FROM客户);而($行= mysql_fetch_assoc($结果))$输出[] = $行;
打印(json_en code($输出));
?>
登录猫错误
一月3日至11日:53:37.569:I /编舞(1765):跳过124帧!该应用程序可能会做它的主线程的工作太多了。
1月3日至一十一日:53:45.780:E / log_tag(1765):错误HTTP connectionjava.lang.IllegalArgumentException:主机名不能为空
1月3日至一十一日:53:45.798:E / log_tag(1765):错误转换resultjava.lang.NullPointerException:锁== NULL
1月3日至一十一日:53:45.798:E /登录TEG(1765):错误解析Dataorg.json.JSONException:在字符输入1结束
1月3日至一十一日:53:45.968:I /编舞(1765):跳过120帧!该应用程序可能会做它的主线程的工作太多了。
1月3日至一十一日:53:46.298:I /编舞(1765):45跳过帧!该应用程序可能会做它的主线程的工作太多了。
这是调用问题 DefaultHttpClient()
法
当您使用的是HTTPS,您需要使用HttpClient的下面,
创建一个单独的方法像下面,
私人DefaultHttpClient getHttpClient()
{
SchemeRegistry schemeRegistry =新SchemeRegistry();
schemeRegistry.register(新计划(https开头,SSLSocketFactory的
.getSocketFactory(),443)); 的HttpParams PARAMS =新BasicHttpParams();
INT timeoutConnection = 30000;
HttpConnectionParams.setConnectionTimeout(参数,可以timeoutConnection);
INT timeoutSocket = 30000;
HttpConnectionParams.setSoTimeout(参数,可以timeoutSocket); params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS,30);
params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE,
新ConnPerRouteBean());
params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE,FALSE);
HttpProtocolParams.setVersion(参数,可以HttpVersion.HTTP_1_1); ThreadSafeClientConnManager厘米=新ThreadSafeClientConnManager(
参数,可以schemeRegistry);
DefaultHttpClient客户端=新DefaultHttpClient(厘米,则params);
client.setRoutePlanner(新DefaultHttpRoutePlanner(schemeRegistry));
返回(客户端);
}
在你的code添加此方法,并修改下面一行,
的HttpClient HttpClient的=新DefaultHttpClient(); HttpClient的HttpClient的=新DefaultHttpClient();
对此,
的HttpClient HttpClient的= getHttpClient();
I am trying to retrieve the data stored in Mysql database using php script and JSON response.But when I run the program I get the output as Could not connect to the database.
MainActivity.java
public class MainActivity extends Activity {
TextView resultView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.enableDefaults();
resultView = (TextView) findViewById(R.id.result);
getData();
}
public void getData() {
String result = " ";
InputStream isr = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://192.168.x.xx/sheetal/getdata.php");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
isr = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection" + e.toString());
resultView.setText("Could'nt connect to database");
}
//convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(isr, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result" + e.toString());
}
//parse json data
try {
String s = "";
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json = jArray.getJSONObject(i);
s = s +
"Name: " + json.getString("Firstname") + " " + json.getString("Lastname") + "\n" +
"Age: " + json.getInt("Age") + "\n" +
"Mobile Using: " + json.getString("Product") + "\n\n";
}
resultView.setText(s);
} catch (Exception e) {
Log.e("log-teg", "Error Parsing Data" + e.toString());
}
}
}
This is my php file getdata.php
<?php
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
die('Could not Connect:' . mysql_error());
}
mysql_select_db("test1", $con);
$result = mysql_query("SELECT * FROM customer");
while ($row = mysql_fetch_assoc($result)) $output[] = $row;
print (json_encode($output));
?>
Log-cat error
03-11 01:53:37.569: I/Choreographer(1765): Skipped 124 frames! The application may be doing too much work on its main thread.
03-11 01:53:45.780: E/log_tag(1765): Error in http connectionjava.lang.IllegalArgumentException: Host name may not be null
03-11 01:53:45.798: E/log_tag(1765): Error converting resultjava.lang.NullPointerException: lock == null
03-11 01:53:45.798: E/log-teg(1765): Error Parsing Dataorg.json.JSONException: End of input at character 1 of
03-11 01:53:45.968: I/Choreographer(1765): Skipped 120 frames! The application may be doing too much work on its main thread.
03-11 01:53:46.298: I/Choreographer(1765): Skipped 45 frames! The application may be doing too much work on its main thread.
It is a problem with calling DefaultHttpClient()
method,
When you are using Https you need to use HttpClient as below,
create a separate method like below,
private DefaultHttpClient getHttpClient()
{
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("https", SSLSocketFactory
.getSocketFactory(), 443));
HttpParams params = new BasicHttpParams();
int timeoutConnection = 30000;
HttpConnectionParams.setConnectionTimeout(params, timeoutConnection);
int timeoutSocket = 30000;
HttpConnectionParams.setSoTimeout(params, timeoutSocket);
params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE,
new ConnPerRouteBean());
params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(
params, schemeRegistry);
DefaultHttpClient client = new DefaultHttpClient(cm, params);
client.setRoutePlanner(new DefaultHttpRoutePlanner(schemeRegistry));
return (client);
}
Add this method in to your code and modify below line,
HttpClient httpclient = new DefaultHttpClient();HttpClient httpclient = new DefaultHttpClient();
To this,
HttpClient httpclient = getHttpClient();
这篇关于连接到https://xxx.x.x.x拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!