首页 \ 问答 \ java.lang.String类型的值无法在Android中转换为JSONArray(重新发布)(Value of type java.lang.String cannot be converted to JSONArray in Android(repost))

java.lang.String类型的值无法在Android中转换为JSONArray(重新发布)(Value of type java.lang.String cannot be converted to JSONArray in Android(repost))

我转发这个因为我找不到解决这个问题的方法......

这是错误

org.json.JSONException:Value <!DOCTYPE of type java.lang.String cannot be converted to JSONArray

这是JSON输出:

[{"name":"Muhaimin","address":"Sylhet","bgroup":"o+"}]

这是类代码:

entpackage com.example.blood;

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.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("NewApi")
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();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

public void getData()
{
 String result="";
 InputStream isr=null;
 try{
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost=new HttpPost("http://10.0.2.2/blood/index.php");
    httppost.setHeader("Content-Type", "application/json");
     httppost.setHeader("Accept", "JSON");
    HttpResponse response=httpclient.execute(httppost);
    HttpEntity entity=response.getEntity();
    isr=entity.getContent();
  }catch(Exception e){
    String err=e.toString();
    resultView.setText("Could not connect to database"+err);
    Log.e("error", err);
  }

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){
    String err2=e.toString();
    resultView.setText("Error coverting result"+err2);
    Log.e("error", err2);
  }
  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("name")+"\n"+
                "Blood Group :"+json.getString("bgroup")+"\n" +
                "Address :"+json.getString("address")+"\n\n";
    }
    resultView.setText(s);
  }catch(Exception e){
    String err1=e.toString();
    resultView.setText("xxx"+err1);
    Log.e("error", err1);
 }
 }

}

这是PHP代码:

  <?php
$con=mysql_connect("localhost","root","","BloodDB");
mysql_select_db("BloodDB",$con);

$result=mysql_query("SELECT name,address,bgroup FROM tbl_blood_donor");

while($row=mysql_fetch_assoc($result))
{
    $output[]=$row;
}
print(json_encode($output));
mysql_close($con);
 ?>

现在如何解决这个问题。我提前在localhost.thanks工作


I am repost this coz I dont find the solution for this problem...

here is error

org.json.JSONException:Value <!DOCTYPE of type java.lang.String cannot be converted to JSONArray

here is JSON output:

[{"name":"Muhaimin","address":"Sylhet","bgroup":"o+"}]

here is class code:

entpackage com.example.blood;

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.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("NewApi")
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();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

public void getData()
{
 String result="";
 InputStream isr=null;
 try{
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost=new HttpPost("http://10.0.2.2/blood/index.php");
    httppost.setHeader("Content-Type", "application/json");
     httppost.setHeader("Accept", "JSON");
    HttpResponse response=httpclient.execute(httppost);
    HttpEntity entity=response.getEntity();
    isr=entity.getContent();
  }catch(Exception e){
    String err=e.toString();
    resultView.setText("Could not connect to database"+err);
    Log.e("error", err);
  }

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){
    String err2=e.toString();
    resultView.setText("Error coverting result"+err2);
    Log.e("error", err2);
  }
  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("name")+"\n"+
                "Blood Group :"+json.getString("bgroup")+"\n" +
                "Address :"+json.getString("address")+"\n\n";
    }
    resultView.setText(s);
  }catch(Exception e){
    String err1=e.toString();
    resultView.setText("xxx"+err1);
    Log.e("error", err1);
 }
 }

}

here is php code:

  <?php
$con=mysql_connect("localhost","root","","BloodDB");
mysql_select_db("BloodDB",$con);

$result=mysql_query("SELECT name,address,bgroup FROM tbl_blood_donor");

while($row=mysql_fetch_assoc($result))
{
    $output[]=$row;
}
print(json_encode($output));
mysql_close($con);
 ?>

Now how to solve this problem.I am working in localhost.thanks in advance


原文:https://stackoverflow.com/questions/21674326
更新时间:2024-02-04 10:02

最满意答案

您的代码似乎缺少可拖动。

看到这段代码:

if( dropped.attr( 'id' ) === 'galleryItem' ) {
            //alert("dropped if");
            var newEl = $( dropped ).clone().appendTo( droppedOn );

            newEl.removeAttr( 'id' ).removeClass( 'thumb' ).html( '<div style="background:red;width:400px;height:50px;"></div>' ).addClass( 'layer-item' );
            newEl.removeClass( 'dragging' );

            newEl.addClass("item");
            $(".item").removeClass("ui-draggable draggable");
            $(".item").draggable({
                cursor: 'move',     
            });

        } else {
            //alert("dropped else");
            $( dropped ).detach().css( { top: 0, left: ui.helper.position().left - 160 } ).appendTo( droppedOn );
        }

Your code seems missing draggable.

See this code:

if( dropped.attr( 'id' ) === 'galleryItem' ) {
            //alert("dropped if");
            var newEl = $( dropped ).clone().appendTo( droppedOn );

            newEl.removeAttr( 'id' ).removeClass( 'thumb' ).html( '<div style="background:red;width:400px;height:50px;"></div>' ).addClass( 'layer-item' );
            newEl.removeClass( 'dragging' );

            newEl.addClass("item");
            $(".item").removeClass("ui-draggable draggable");
            $(".item").draggable({
                cursor: 'move',     
            });

        } else {
            //alert("dropped else");
            $( dropped ).detach().css( { top: 0, left: ui.helper.position().left - 160 } ).appendTo( droppedOn );
        }

相关问答

更多
  • 这是一个非常简单的例子,可能让你开始: $(document).ready(function() { var $dragging = null; $(document.body).on("mousemove", function(e) { if ($dragging) { $dragging.offset({ top: e.pageY, left: e.pageX ...
  • 尝试 $('.drop').droppable({ tolerance: 'intersect', drop: function(event, ui) { console.log(this.id)// this.id will give the `.drop` id to which the element was dropped var drop_p = $(this).offset(); var drag_p = ui.draggable. ...
  • 使用这种方法: ui.draggable.attr('id') Use this method: ui.draggable.attr('id')
  • 当你将droppable放入drop target后稍微移动它并且它失去了可拖动性,那是因为 $this.html(""); 在drop handler中,draggable仍然在drop目标中。 擦除放置目标的HTML时,还会删除应该重新附加的元素。 这会返回一个语法错误,因为该元素不再存在,从而中断了克隆的操作,并且可擦除了拖动。 这是一个快速修复: drop: function(event, ui) { var $this = $(this); if ($this.find('.u ...
  • 刚刚删除destroy draggable on drop event这里正在运行jsfiddle drop: function( event, ui ) { var text = ui.draggable.html(); $(".cart").append('
    '+text+'
    '); ui.draggable.data('dropped', true); $(ui.draggable).draggable({ ...
  • 试试这个: http : //jsfiddle.net/6JtMp/ 代码如下: