首页 \ 问答 \ localhost在android studio中拒绝了(localhost refused in android studio)

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
更新时间:2023-07-03 13:07

最满意答案

好。 问题出在WorldPay上,因为他们没有提供所需的权限!


Ok. The problem were with WorldPay as they did not provide required permissions!

相关问答

更多
  • 您可以只读域,这将不允许更改您的控件的值。 编辑:您可以轻松地编写一个“ serializeDisabled ”函数,迭代具有name属性的禁用表单元素并在最后使用jQuery.param函数来生成序列化字符串: (function ($) { $.fn.serializeDisabled = function () { var obj = {}; $(':disabled[name]', this).each(function () { obj[this.name] ...
  • 为了根据模块名称为当前网站动态检索提供程序,我创建了这个帮助程序方法。 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 ...
  • 您需要在模型上实现__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"]'); ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)