2016-06-10 2 views
0

Ive는 여기에있는 모든 솔루션을 시도했지만 나에게는 아무런 효과가없는 것으로 보입니다. 나는 안드로이드를 처음 접했고 이것이 내 문제 다.JSON으로 안드로이드 애플 리케이션에 배열을 건네기

나는 그것이 나에게주는 MySQL의 데이터를 가져 와서 PHP 스크립트가 실행되는 배열

<?php 

$con = mysqli_connect("localhost", "username", "passwod", "database"); 
$result = array(); 
$getacceptedsotres = mysqli_query($con, "SELECT * FROM offer WHERE  
type='sale' AND approved = 'approved'"); 
while($rowgetid = mysqli_fetch_assoc($getacceptedsotres)){ 

$storeid = $rowgetid['id']; 

$getstores = mysqli_query($con, "SELECT * FROM sale WHERE offerid = 
'$storeid' "); 

while($row = mysqli_fetch_assoc($getstores)){ 

    array_push($result,array(
    'title'=>$row['title'], 
    'offerto' => $rowgetid['offerto'], 
    'type' => $rowgetid['type'], 
    'percentageoff'=>$row['percentageoff'] 

)); 

} 
} 

    echo json_encode(array($result)); 

로 전달이 PHP 스크립트를이 :

[[{ "제목" "offer": "06/29 /2016", "type": "sale", "percentageoff": "40 %"}, { "title": "30 % sale on" "offer": "07/31/2016", "type": "sale", "percentageoff": "30 %"}, { "title": "선택한 항목에 45 % 할인", "offerto": "07/08 /2016", "type": "sale ","percentoff ":"45 "}]]

내 결과는이 결과를 android studio의 목록에 전달하려는 경우입니다.

공용 클래스 세일 내가 응용 프로그램을 실행하고 나는 '결과를 나열 할 때 AppCompatActivity가 {

private ArrayList<GetSale> saleList; 
private ListView lvSale; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_sale); 

    ImageLoader.getInstance().init(UILConfig.config(Sale.this)); 

    PostResponseAsyncTask taskRead = new PostResponseAsyncTask(Sale.this, this); 

    taskRead.execute("http://....php"); 




} 

@Override 
public void processFinish(String s) { 

    saleList = new JsonConverter<GetSale>().toArrayList(s, GetSale.class); 
    BindDictionary<GetSale> dict = new BindDictionary<GetSale>(); 


    dict.addStringField(R.id.tvSaleAmount, new StringExtractor<GetSale>() { 
     @Override 
     public String getStringValue(GetSale getSale, int position) { 
      return getSale.title; 
     } 
    }); 

    dict.addStringField(R.id.tvType, new StringExtractor<GetSale>() { 
     @Override 
     public String getStringValue(GetSale getSale, int position) { 
      return getSale.type; 
     } 
    }); 

    FunDapter<GetSale> adapter = new FunDapter<>(
      Sale.this, saleList, R.layout.layout_sale, dict); 

    lvSale = (ListView)findViewById(R.id.lvSale); 
    lvSale.setAdapter(adapter); 


} 

}

그래서 AsyncResponse를 구현 확장 내 수업에서 사용되는 코드입니다 내 PHP 스크립트에서 점점 내가 안드로이드 스튜디오 에서이 긴 오류가 발생하지만 난이 오류를 일으키는 메인 라인이라고 생각

>06-10 17:34:17.262 28516-28516/com.example.W/System.err: 
>com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected 
>BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 3 
>06-10 17:34:17.262 28516-28516/com.example.W/System.err:  at 
>com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(Reflect>iveTypeAdapterFactory.java:176) 
>06-10 17:34:17.262 28516-28516/com.example.W/System.err:  at 
>com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRun>timeTypeWrapper.java:40) 

>. 
>. 
>. 

>06-10 17:34:17.272 28516-28516/com.example.W/System.err: ... 21 more 
>06-10 17:34:17.282 28516-28516/com.example.E/ViewRootImpl: >sendUserActionEvent() mView == null 

도움이 될 것입니다. 사전에

+1

PHP 코드에서'echo json_encode (array ($ result));'를'echo json_encode ($ result);'행으로 변경하십시오. –

+0

@PrerakSola omg it worked .. 배열의 종류가 다르며 echo json_encode ($ result)와 작동하지 않습니다. 하지만 배열을 변경하고 다시이 에코를 시도하는 것을 잊었습니다 –

+0

도와 드리겠습니다 .. :) –

답변

0

변경 PHP 코드에서

echo json_encode($result);에 선

echo json_encode(array($result));

을 주셔서 감사합니다.

$result은 처음에 $result = array();처럼 배열입니다. son_encode(array($result))을 수행하면 배열 객체를 다른 배열에 통합합니다.

JSON 응답 시작시 [[ 알림. 그것 때문입니다. 그리고 안드로이드 측에서는 외부 배열을 비 직렬화했습니다. 그렇기 때문에 Expected BEGIN_OBJECT but was BEGIN_ARRAY 예외가 발생했습니다.

관련 문제