我正在创建程序,我可以从服务器解析XML数据。我从服务器获得响应,但输出包含Unicode。
我在下面附上我的代码
package com.example.rsdevteam.xmlparsing;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends Activity
{
URL url=null;
HttpURLConnection con=null;
String u="http://charts.jmfonline.in/OMSRouter/OrderRouter.svc/LedgBal_3";
StringBuffer response = new StringBuffer();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new MyTask().execute();
}
public class MyTask extends AsyncTask<String,Void,String>
{
String param=null;
@Override
protected String doInBackground(String... params) {
try
{
url=new URL(u);
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/json");
JSONObject param = new JSONObject();
param.put("SrvName","");
param.put("MethodName","");
param.put("Parameters","08 Mar 2017|07 Apr 2017|1|1|1|10112829|0|");
OutputStreamWriter os = new OutputStreamWriter(con.getOutputStream());
os.write(param.toString());
os.flush();
os.close();
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK)
{
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"utf-8"));
String line = null;
while ((line = br.readLine()) != null)
{
response.append(line);
}
br.close();
System.out.println(response.toString());
}
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s)
{
super.onPostExecute(s);
}
}
}
我得到的输出是:
"<NewDataSet>\u000d\u000a <Table>\u000d\u000a <RowSeg1>1<\/RowSeg1>\u000d\u000a <RowSeg>1<\/RowSeg>\u000d\u000a <Exchange>NSE<\/Exchange>\u000d\u000a <Segment>CAPITAL<\/Segment>\u000d\u000a <CltCode>10112829 <\/CltCode>\u000d\u000a <CLTNAME>MEHUL ULLHAS PARIKH<\/CLTNAME>\u000d\u000a <ClientCodeAndName>MEHUL ULLHAS PARIKH ( 10112829 ) <\/ClientCodeAndName>\u000d\u000a <DATE>2017-03-08T00:00:00+05:30<\/DATE>\u000d\u000a <Vtype>18<\/Vtype>\u000d\u000a <SHORTDESC>OPENEN<\/SHORTDESC>\u000d\u000a <Particular>Opening balance<\/Particular>\u000d\u000a <VoucherNumber \/>\u000d\u000a <Debit>0.00<\/Debit>\u000d\u000a <Credit>28.04<\/Credit>\u000d\u000a <OpeningBalance>-28.04<\/OpeningBalance>\u000d\u000a <Running_x0020_Balance>28.04<\/Running_x0020_Balance>\u000d\u000a <ChequeNumber \/>\u000d\u000a <\/Table>\u000d\u000a <Table>\u000d\u000a <RowSeg1>2<\/RowSeg1>\u000d\u000a <RowSeg>2<\/RowSeg>\u000d\u000a <Exchange>NSE<\/Exchange>\u000d\u000a <Segment>CAPITAL<\/Segment>\u000d\u000a <CltCode>10112829 <\/CltCode>\u000d\u000a <CLTNAME>MEHUL ULLHAS PARIKH<\/CLTNAME>\u000d\u000a <ClientCodeAndName>MEHUL ULLHAS PARIKH ( 10112829 ) <\/ClientCodeAndName>\u000d\u000a <DATE>2017-03-17T00:00:00+05:30<\/DATE>\u000d\u000a <Vtype>3<\/Vtype>\u000d\u000a <SHORTDESC>PAYBNK<\/SHORTDESC>\u000d\u000a <Particular>QUARTERLY SETTLEMENT - R<\/Particular>\u000d\u000a <VoucherNumber>201600138689<\/VoucherNumber>\u000d\u000a <Debit>28.04<\/Debit>\u000d\u000a <Credit>0.00<\/Credit>\u000d\u000a <OpeningBalance>-28.04<\/OpeningBalance>\u000d\u000a <Running_x0020_Balance>0.00<\/Running_x0020_Balance>\u000d\u000a <ChequeNumber>4440U17077872233<\/ChequeNumber>\u000d\u000a <\/Table>\u000d\u000a <Table>\u000d\u000a <RowSeg1>3<\/RowSeg1>\u000d\u000a <RowSeg>3<\/RowSeg>\u000d\u000a <Exchange>NSE<\/Exchange>\u000d\u000a <Segment>CAPITAL<\/Segment>\u000d\u000a <CltCode>10112829 <\/CltCode>\u000d\u000a <CLTNAME>MEHUL ULLHAS PARIKH<\/CLTNAME>\u000d\u000a <ClientCodeAndName>MEHUL ULLHAS PARIKH ( 10112829 ) <\/ClientCodeAndName>\u000d\u000a <DATE>2017-03-21T00:00:00+05:30<\/DATE>\u000d\u000a <Vtype>2<\/Vtype>\u000d\u000a <SHORTDESC>REPBNK<\/SHORTDESC>\u000d\u000a <Particular>ONLINE PAYIN<\/Particular>\u000d\u000a <VoucherNumber>201600103155<\/VoucherNumber>\u000d\u000a <Debit>0.00<\/Debit>\u000d\u000a <Credit>50.00<\/Credit>\u000d\u000a <OpeningBalance>-28.04<\/OpeningBalance>\u000d\u000a <Running_x0020_Balance>50.00<\/Running_x0020_Balance>\u000d\u000a <ChequeNumber>1172615900<\/ChequeNumber>\u000d\u000a <\/Table>\u000d\u000a<\/NewDataSet>"
/u000d/u000a
这应该被删除,所有标记都会像<\/TAG_NAME>
一样关闭,但标记应该像</TAG_Name>
答案 0 :(得分:0)
尝试使用org.apache.commons.lang.StringEscapeUtils.unescapeJava(your_string);
您可以在http://www.java2s.com/Code/Jar/a/Downloadapachecommonslangjar.htm
找到jar文件