2011-03-31 3 views
3

다음은 코드입니다.안드로이드에서 kSOAP을 사용하여 double 값을 직렬화하는 방법

package com.SRS6; 

import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.SoapObject; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.ksoap2.transport.HttpTransportSE; 
import android.app.Activity; 
import android.database.Cursor; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

public class result extends Activity { 


    private static final String SOAP_ACTION = "http://tempuri.org/insertquizdata";  
    private static final String METHOD_NAME = "insertquizdata";  
    private static final String NAMESPACE = "http://tempuri.org/";  
    private static final String URL = "http://192.168.1.203/studentresponse/Service.asmx"; 


    TextView txtcorrect; 
    TextView txtwrong; 
    TextView txttime; 
    TextView txttotal; 
    TextView txtunattempted,tv; 
    ImageView resimg; 
// TextView txttimer; 

    String n1; 
    int n2; 
    double n3; 

    DataBaseHelper data; 



    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.result); 

     data=new DataBaseHelper(this); 

     //resimg=(ImageView)findViewById(R.id.img); 
     tv=(TextView)findViewById(R.id.tv); 
     txttotal=(TextView)findViewById(R.id.ua); 
     txttotal.setTextColor(Color.BLACK); 
     txtunattempted=(TextView)findViewById(R.id.ua1); 
     txtunattempted.setTextColor(Color.BLACK); 
     txtcorrect=(TextView)findViewById(R.id.correct); 
     txtcorrect.setTextColor(Color.BLACK); 
     txtwrong=(TextView)findViewById(R.id.wrong); 
     txtwrong.setTextColor(Color.BLACK); 
     txttime=(TextView)findViewById(R.id.time3); 
     txttime.setTextColor(Color.BLACK); 
     // txttimer=(TextView)findViewById(R.id.time4); 
     // txttimer.setTextColor(Color.BLACK); 

     Bundle bundle = getIntent().getExtras(); 
     String stuname=bundle.getString("stuname"); 
     System.out.println("Student name:"+stuname); 
     int tesid=bundle.getInt("testid",0); 
     System.out.println("Test id:"+tesid); 
     int value = bundle.getInt("correctcount", 0); 
     int value1 = bundle.getInt("wrongcount", 0); 
     int value2=bundle.getInt("Ques",0); 
     int value3=bundle.getInt("unatt",0); 

     double perce=(value*100)/value2; 
     System.out.println("Percentage:"+perce); 

     String time2 = bundle.getString("time"); 
     //String time3 = bundle.getString("timer"); 


     txttotal.setText(" "+value2+" No of questions"); 
     txtunattempted.setText(" "+value3+" Unattempted"); 
     txtcorrect.setText(" "+value+" right answers"); 
     txtwrong.setText(" "+value1+" wrong answers"); 
     txttime.setText(" "+time2); 
     // txttimer.setText(" "+time3); 


     data.Insertfinalresult(stuname, tesid, perce); 
     getfinalresult(); 
     // call(); 


} 

    public void call() 
    { 
     try { 


       SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 


       request.addProperty("strusername",n1); 

       request.addProperty("inttestid",n2); 

       request.addProperty("dblper",n3); 


       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

       envelope.setOutputSoapObject(request); 
       envelope.dotNet=true; 
       envelope.encodingStyle = SoapSerializationEnvelope.XSD; 
       HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

       androidHttpTransport.call(SOAP_ACTION, envelope); 
       Toast.makeText(result.this, "Data posted successfully",Toast.LENGTH_SHORT).show(); 

     } 
      catch (Exception e)  
      { 
      tv.setText(e.getMessage()); 
      } 
    } 

    public void getfinalresult() 
    { 
     Cursor c1=data.selectfinalresult(); 
      while(c1.moveToNext()) 
      { 
       n1=c1.getString(0); 
       n2=c1.getInt(1); 
       n3=c1.getDouble(2); 
       System.out.println("First:"+n1); 
       System.out.println("Second:"+n2); 
       System.out.println("Third:"+n3); 
       call(); 
      } 
    } 
} 

kSOAP을 사용하여 double 값을 직렬화하는 방법을 알려주십시오.

답변

5

Double 및 Date의 데이터 형식을 수동으로 마샬링해야합니다. 샤프 기어 (Sharp Gears)를 보시려면 여기를 클릭하십시오. Double을 마샬링하는 링크는 Implementing KSOAP Marshal Interface입니다.

+0

kSOAP2에서 MarshalDouble을 사용해 보셨습니까? 그 클래스는 사라진 것으로 보이고 Android Studio를 사용할 때 MarshalFloat로 대체되었습니다 ... – whyoz

4

Android Studio에서 kSOAP2 및 API 19로 MarshalDouble을 사용하려고하면 MarshalFloat 만 사용 가능한 클래스로 인식됩니다. 이 코드를 실행하면 프로젝트에이 코드를 "MarshalDouble.java"라는 자체 자바 클래스에 추가하면됩니다.

import java.io.IOException; 
import org.ksoap2.serialization.Marshal; 
import org.ksoap2.serialization.PropertyInfo; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.xmlpull.v1.XmlPullParser; 
import org.xmlpull.v1.XmlPullParserException; 
import org.xmlpull.v1.XmlSerializer; 

public class MarshalDouble implements Marshal 
{ 


public Object readInstance(XmlPullParser parser, String namespace, String name, 
     PropertyInfo expected) throws IOException, XmlPullParserException { 

    return Double.parseDouble(parser.nextText()); 
} 


public void register(SoapSerializationEnvelope cm) { 
    cm.addMapping(cm.xsd, "double", Double.class, this); 

} 


public void writeInstance(XmlSerializer writer, Object obj) throws IOException { 
     writer.text(obj.toString()); 
    } 

} 

당신은 다음과 같은 봉투를 등록 할 수 있습니다 :

MarshalDouble md = new MarshalDouble(); 
md.register(envelope); 

이것은 당신이 웹 서비스 당신에게에서 위도와 경도 값을 받고있어 경우에 유용하고있는 데이터베이스를 업데이트해야 할 수 유효한 double 치

관련 문제