localhost在android studio中拒绝了(localhost refused in android studio)
我正在使用wampserver并尝试使用php脚本通过json解析从服务器获取记录。 我想在我的Android应用程序上显示该数据....
我的脚本代码很清楚,它工作得很完美。而且我也很清楚android代码。 但是,当我运行我的应用程序时,数据不会显示在我的应用程序上....
这是我的android代码....
package com.example.abc.testdatabase; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.os.StrictMode; import android.util.Log; import android.widget.TextView; public class TestExternalDatabaseActivity extends Activity { /** Called when the activity is first created. */ TextView resultView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); StrictMode.enableDefaults(); //STRICT MODE ENABLED resultView = (TextView) findViewById(R.id.result); getData(); } public void getData(){ String result = ""; InputStream isr = null; try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://10.0.2.2/getAllCustomers.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("Couldnt 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"+ "Qualification : "+json.getInt("Qualification")+"\n"+ "Mobile Using : "+json.getString("Mobile")+"\n\n"; } resultView.setText(s); } catch (Exception e) { // TODO: handle exception Log.e("log_tag", "Error Parsing Data "+e.toString()); } } }
以下是我面临的错误...........
Error Parsing Data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray
以下是我的PHP脚本...........
<?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("testDatabase", $con); $result = mysql_query("SELECT * FROM Customer"); while($row = mysql_fetch_assoc($result)) { $output[]=$row; } print(json_encode($output)); mysql_close($con); ?>
我不明白为什么我没有得到任何记录....如果有人围绕这个,那么请帮助这个.....
Log.e................ <br /> <font size='1'><table class='xdebug-error xe-deprecated' dir='ltr' border='1' cellspacing='0' cellpadding='1'> <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! ) </span> Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp\www\TestDatabase\connection.php on line <i>3</i></th></tr> <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr> <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr> <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0170</td><td bgcolor='#eeeeec' align='right'>135864</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp\www\TestDatabase\getAllCustomer.php' bgcolor='#eeeeec'>..\getAllCustomer.php<b>:</b>0</td></tr> <tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0190</td><td bgcolor='#eeeeec' align='right'>137344</td><td bgcolor='#eeeeec'>include_once( <font color='#00bb00'>'C:\wamp\www\TestDatabase\connection.php'</font> )</td><td title='C:\wamp\www\TestDatabase\getAllCustomer.php' bgcolor='#eeeeec'>..\getAllCustomer.php<b>:</b>4</td></tr> <tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>0.0190</td><td bgcolor='#eeeeec' align='right'>137528</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.mysql-connect' target='_new'>mysql_connect</a&t; ( ) </td><td title='C:\wamp\www\TestDatabase\connection.php' bgcolor='#eeeeec'>..\connection.php<b>:</b>3</td></tr> </table></font> [{"FirstName":"Vandana ","LastName":"Rao","Qualification":"MscICT","Mobile":"Sony Xepri"}] 03-23 10:29:26.965 2426-2426/com.example.abc.testdatabase E/log_tag﹕ Error Parsing Data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray
I am using wampserver and trying fetch record from server using php script through json parsing. And I want to display that data on my android app....
My script code is clear and it worked perfect..And I am also clear with android code. But when I run my app the data is not display on my app....
This is my android code....
package com.example.abc.testdatabase; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.os.StrictMode; import android.util.Log; import android.widget.TextView; public class TestExternalDatabaseActivity extends Activity { /** Called when the activity is first created. */ TextView resultView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); StrictMode.enableDefaults(); //STRICT MODE ENABLED resultView = (TextView) findViewById(R.id.result); getData(); } public void getData(){ String result = ""; InputStream isr = null; try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://10.0.2.2/getAllCustomers.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("Couldnt 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"+ "Qualification : "+json.getInt("Qualification")+"\n"+ "Mobile Using : "+json.getString("Mobile")+"\n\n"; } resultView.setText(s); } catch (Exception e) { // TODO: handle exception Log.e("log_tag", "Error Parsing Data "+e.toString()); } } }
Following is error I am facing...........
Error Parsing Data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray
Following is my php script...........
<?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("testDatabase", $con); $result = mysql_query("SELECT * FROM Customer"); while($row = mysql_fetch_assoc($result)) { $output[]=$row; } print(json_encode($output)); mysql_close($con); ?>
I don't understand why I am not getting any records.... If anyone round this then please help for this.....
Log.e................ <br /> <font size='1'><table class='xdebug-error xe-deprecated' dir='ltr' border='1' cellspacing='0' cellpadding='1'> <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! ) </span> Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp\www\TestDatabase\connection.php on line <i>3</i></th></tr> <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr> <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr> <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0170</td><td bgcolor='#eeeeec' align='right'>135864</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp\www\TestDatabase\getAllCustomer.php' bgcolor='#eeeeec'>..\getAllCustomer.php<b>:</b>0</td></tr> <tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0190</td><td bgcolor='#eeeeec' align='right'>137344</td><td bgcolor='#eeeeec'>include_once( <font color='#00bb00'>'C:\wamp\www\TestDatabase\connection.php'</font> )</td><td title='C:\wamp\www\TestDatabase\getAllCustomer.php' bgcolor='#eeeeec'>..\getAllCustomer.php<b>:</b>4</td></tr> <tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>0.0190</td><td bgcolor='#eeeeec' align='right'>137528</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.mysql-connect' target='_new'>mysql_connect</a> ( ) </td><td title='C:\wamp\www\TestDatabase\connection.php' bgcolor='#eeeeec'>..\connection.php<b>:</b>3</td></tr> </table></font> [{"FirstName":"Vandana ","LastName":"Rao","Qualification":"MscICT","Mobile":"Sony Xepri"}] 03-23 10:29:26.965 2426-2426/com.example.abc.testdatabase E/log_tag﹕ Error Parsing Data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray
原文:https://stackoverflow.com/questions/29206582
最满意答案
好。 问题出在WorldPay上,因为他们没有提供所需的权限!
Ok. The problem were with WorldPay as they did not provide required permissions!
相关问答
更多-
提交禁用的字段(submit disabled fields)[2022-01-26]
您可以只读域,这将不允许更改您的控件的值。 编辑:您可以轻松地编写一个“ serializeDisabled ”函数,迭代具有name属性的禁用表单元素并在最后使用jQuery.param函数来生成序列化字符串: (function ($) { $.fn.serializeDisabled = function () { var obj = {}; $(':disabled[name]', this).each(function () { obj[this.name] ... -
动态模块数据未显示在sitefinity的管理面板中(Dynamic Module data not showing up in the administration panel of sitefinity)[2022-11-02]
为了根据模块名称为当前网站动态检索提供程序,我创建了这个帮助程序方法。 public static DynamicModuleManager GetDynamicProvider(string moduleName) { // Set the provider name for the DynamicModuleManager here. All available providers are listed in // Administrati ... -
“错误”结果与捕获/结算付款有关,因此它与您的网站与WorldPay的集成方式中的错误无关。 不幸的是,如果用户成功收费(这将是'CAPTURED')或者他们点击取消,WorldPay只会向您发送“付款响应”。 就像你在评论中所说的那样,如果用户点击“取消”,用户只会转到结果C.html。 如果WorldPay无法访问您的回调网址,您可能还会收到“回拨失败”。 一般来说,如果在WorldPay结束时创建收费有错误,他们不会让您知道(同样,当收费结算或拒绝时,它不会让您知道)。 在过去的两年里,我们从来没有创 ...
-
好。 问题出在WorldPay上,因为他们没有提供所需的权限! Ok. The problem were with WorldPay as they did not provide required permissions!
-
这是Android SDK的一个问题。 最新版本的Android无法正常运行。 我在Eclipse的布局视图中改回了18级(从19,Android 4.4),它再次运行。 It was a problem with the Android SDK. The newest version of Android wasn't working correctly. I changed back to level 18 (from 19, Android 4.4) in the layout view in Ecl ...
-
一般来说,我会说最好为此目的创建一个自定义视图(在网站的公共部分)。 但是如果您更喜欢使用admin,那么您可以尝试使用get_queryset方法。 例如,如果您想向管理员显示所有帖子以及为简单用户显示他们自己的帖子,那么就像这样(从上面的官方文档链接复制): class PostAdmin(admin.ModelAdmin): def get_queryset(self, request): qs = super(PostAdmin, self).get_queryset(req ...
-
Django博客管理(Django blog administration)[2022-03-10]
您需要在模型上实现__unicode__方法。 class Post(models.Model): title = models.CharField(max_length=255) datetime = models.DateTimeField(u'Date of publishing') content = models.TextField(max_length=10000) def __unicode__(self): return self.title ... -
您应该将Ids设置为您的组件: var titlePanel = new Ext.Panel({ id: 'titlePanel', }); var descPanel = new Ext.Panel({ id: 'descPanel', }); 并通过此ID在您需要的地方获取: var tP = Ext.getCmp('titlePanel'); 然后将新组件添加到面板并更新布局,如下所示: tP.add(new Ext.form.TextField({ fieldLab ...
-
地址字段与国家地址格式直接相关。 在显示其他字段电话字段之前,转到菜单客户端 - >地址 - >编辑结束。 一旦找到该特定地址的国家。 然后转到菜单本地化 - >国家,搜索上一个国家/单击编辑。 在地址格式字段上,确保选择了手机和手机。 如果没有,只需添加并保存更改即可。 这就是全部。 您应该在“地址编辑”选项中看到该字段。 如果您看不到它,则必须在商店源代码中搜索此行为的更改或覆盖。 祝你好运。 It seems its not possible using Prestashop's default be ...
-
在HTML表单中disabled的字段无法使用,无法点击且无法提交数据。 关于这样一个领域的观点是,在该字段不再被disabled之前必须发生其他事情 - 然后它变成普通的HTML表单字段。 例如。 var otherReasonRadio = document.querySelector('input[value="reason-other"]'); var otherReasonInput = document.querySelector('input[name="other-reason"]'); ...