[英]Android POST to WCF Service crashes (org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject)
[英]org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject in Android studio
我正在尝试使用 c# 的 REST 服务在 android 应用程序中获取 json 数据。 它抛出一个错误:
org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject
我在上一个问题中搜索了 SO,但找不到任何有用的信息。
我尝试过的是:-
在 C# 中
public DataTable TestCheckEgrasUserLogin()
{
DataTable dt = new DataTable();
GenralFunction gf = new GenralFunction();
SqlParameter[] PM = new SqlParameter[2];
PM[0] = new SqlParameter("@UserName", SqlDbType.VarChar, 50) { Value = UserName };
PM[1] = new SqlParameter("@Password", SqlDbType.VarChar, 50) { Value = Password };
dt = gf.Filldatatablevalue(PM, "TestegAndroidUserLoginInfo", dt, null);
return dt;
}
我已经通过在 Visual Studio 中运行对其进行了测试,它运行良好并带来数据。
在安卓中:-
public class MainActivity extends Activity {
private final static String SERVICE_URI = "`serviceUrl`";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try
{
final EditText usernametxt = (EditText) findViewById(R.id.txtUser);
final EditText passwordtxt = (EditText) findViewById(R.id.txtPassword);
String username;
String Password;
username=usernametxt.getText().toString();
Password=passwordtxt.getText().toString();
if(username=="" || Password=="")
{
MessageBox("Please Enter UserName or Password");
}
else if (username.length()<1 || Password.length()<1)
{
MessageBox("Please Enter Credential Details");
}
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());
final HttpGet request = new HttpGet(SERVICE_URI+username+"/Password/"+Password);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type","application/json; charset=utf-8");
final DefaultHttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
final HttpResponse response = httpClient.execute(request);
final HttpEntity responseEntity = response.getEntity();
// Read response data into buffer
char[] buffer = new char[(int)responseEntity.getContentLength()];
String tmpStr10 = String.valueOf(buffer.length);
InputStream stream = responseEntity.getContent();
InputStreamReader reader = new InputStreamReader(stream);
int sizeOfJSONFile = stream.available();
reader.read(buffer);
stream.close();
JSONObject vehicle = new JSONObject(new String(buffer)); ** /////Error Comes here**
JSONArray plates=new JSONArray(vehicle.getString("CheckEgrasLoginResult"));
Bundle B=new Bundle();
String UserName= null;
String UserId =null;
Intent i = new Intent(getApplicationContext(), com.example.nic.newdemosecond.detailsact.class);
for (int j = 0; j < plates.length(); ++j) {
JSONObject Veh = new JSONObject(plates.getString(j));
UserName = Veh.getString("UserName");
UserId = Veh.getString("UserId");
}
i.putExtra("UserName", UserName);
i.putExtra("UserId", UserId);
startActivity(i);
}
catch (Exception e)
{
MessageBox("Invalid Login");
}
}
}) ;
}
错误出现在我声明JsonObject
。 由于我是 android 新手,我无法捕捉到这个错误。
任何帮助,将不胜感激 !
HttpClient、DefaultHttpClient 已弃用,它们会耗尽电池电量,并在 android 中增加带宽。 使用 HttpURLConnection 而不是按照本教程http://terrapinssky.blogspot.in/2015/10/android-get-and-parse-json-file-from.html
您必须使用JavaScriptSerializer
返回 Json 而不是XML
,如下所示
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
最后返回 Json 之类的,
return serializer.Serialize(rows);
注意:有关详细信息,请参阅此答案,并根据需要对其进行修改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.