我最近开始学习和改造工作。 “获取”方法工作正常。但是当我使用“Post”方法时,它会给我HTML页面作为回复。
我的代码是:
ServiceClient:
public interface ServiceClient {
String SERVICE_ENDPOINT = "https://ghrth.in/rjk/AppServices.asmx";
@POST("/user_v2")
void getUserInfo(@Body TypedInput in, Callback<JsonObject> callback);
}
ServiceUtil:
public class ServiceUtil {
public static ServiceClient getServiceClient() {
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(
ServiceClient.SERVICE_ENDPOINT)
.setLogLevel(LogLevel.FULL)
.build();
ServiceClient serviceClient = restAdapter.create(ServiceClient.class);
return serviceClient;
}
public static class TypedJsonString extends TypedString {
public TypedJsonString(String body) {
super(body);
}
@Override
public String mimeType() {
return "application/json";
}
}
}
的活动:
login_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (emailid.getText().toString().length() == 0 || password.getText().toString().length() == 0
) {
Toast.makeText(getApplicationContext(), "Enter All Fields",
Toast.LENGTH_LONG).show();
} else if (!validEmail(emailid.getText().toString())) { //if (!emailStr.matches("[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+")) {
Toast.makeText(getApplicationContext(),
"Enter valid email id", Toast.LENGTH_LONG).show();
} else {
boolean flag = checkNet(getApplicationContext());
if (flag) {
enableGCM();
onSucceeded();
} else {
showAlert(Login_Activity.this, "No Internet Access");
}
}
}
});
public void onSucceeded() {
ServiceClient serviceClient = ServiceUtil.getServiceClient();
JSONObject userDetails = new JSONObject();
try {
userDetails.put("Emailid", emailid.getText().toString());
userDetails.put("Password", password.getText().toString());
userDetails.put("DeviceID", DEVICEID);
userDetails.put("PlatformID", WebUrl.platformid);
userDetails.put("DeviceToken", regid);
userDetails.put("Mobilemodel", Mobilemodel);
userDetails.put("Appversion", "1.2.8.2");
userDetails.put("MobileOS", MobileOs);
Log.v("TAG_USERDEATILS",""+userDetails);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
serviceClient.getUserInfo(new ServiceUtil.TypedJsonString(userDetails.toString()),
callback);
}
Callback<JsonObject> callback = new Callback<JsonObject>() {
@Override
public void success(JsonObject resultUserInfo, Response response) {
Log.v("TAG_SUCCESS",""+resultUserInfo);
}
@Override
public void failure(RetrofitError error) {
}
};
logcat的:
08-20 11:29:35.751 31958-32374/com.rrtyu.reg D/Retrofit﹕ <--- HTTP 500 https://ghrth.in/rjk/AppServices.asmx/user_v2 (963ms)
08-20 11:29:35.751 31958-32374/com.rrtyu.reg D/Retrofit﹕ : HTTP/1.1 500 Internal Server Error
08-20 11:29:35.751 31958-32374/com.reloadapp.reload D/Retrofit﹕ Access-Control-Allow-Origin: *
08-20 11:29:35.751 31958-32374/com.reloadapp.reload D/Retrofit﹕ Cache-Control: private
08-20 11:29:35.751 31958-32374/com.rrtyu.reg D/Retrofit﹕ Content-Length: 5041
08-20 11:29:35.751 31958-32374/com.rrtyu.reg D/Retrofit﹕ Content-Type: text/html; charset=utf-8
08-20 11:29:35.751 31958-32374/com.rrtyu.reg D/Retrofit﹕ Date: Thu, 20 Aug 2015 05:53:51 GMT
08-20 11:29:35.751 31958-32374/com.rrtyu.reg D/Retrofit﹕ ETag: ""
08-20 11:29:35.751 31958-32374/com.rrtyu.regD/Retrofit﹕ Server: Microsoft-IIS/8.0
08-20 11:29:35.751 31958-32374com.rrtyu.reg D/Retrofit﹕ X-Android-Received-Millis: 1440050375757
08-20 11:29:35.751 31958-32374/com.reloadapp.reload D/Retrofit﹕ X-Android-Sent-Millis: 1440050374825
08-20 11:29:35.751 31958-32374/com.rrtyu.reg D/Retrofit﹕ X-AspNet-Version: 4.0.30319
08-20 11:29:35.761 31958-32374/com.rrtyu.reg D/Retrofit﹕ <!DOCTYPE html>
<html>
<head>
<title>Only Web services with a [ScriptService] attribute on the class definition can be called from script.</title>
<meta name="viewport" content="width=device-width" />
<style>
body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}
p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
pre {font-family:"Consolas","Lucida Console",Monospace;font-size:11pt;margin:0;padding:0.5em;line-height:14pt}
.marker {font-weight: bold; color: black;text-decoration: none;}
.version {color: gray;}
.error {margin-bottom: 10px;}
.expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
@media screen and (max-width: 639px) {
pre { width: 440px; overflow: auto; white-space: pre-wrap; word-wrap: break-word; }
}
@media screen and (max-width: 479px) {
pre { width: 280px; }
}
</style>
</head>
<body bgcolor="white">
<span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>
<h2> <i>Only Web services with a [ScriptService] attribute on the class definition can be called from script.</i> </h2></span>
<font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">
<b> Description: </b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
<br><br>
<b> Exception Details: </b>System.InvalidOperationException: Only Web services with a [ScriptService] attribute on the class definition can be called from script.<br><br>
<b>Source Error:</b> <br><br>
<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code>
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.</code>
</td>
</tr>
</table>
<br>
<b>Stack Trace:</b> <br><br>
<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code><pre>
[InvalidOperationException: Only Web services with a [ScriptService] attribute on the class definition can be called from script.]
System.Web.Script.Services.WebServiceData..ctor(Type type, Boolean pageMethods) +774816
System.Web.Script.Services.WebServiceData.GetWebServiceData(HttpContext context, String virtualPath, Boolean failIfNoData, Boolean pageMethods, Boolean inlineScript) +304
System.Web.Script.Services.RestHandler.CreateHandler(HttpContext context) +136
System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated) +89
System.Web.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +516
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288
</pre></code>
</td>
</tr>
</table>
<br>
<hr width=100% size=1 color=silver>
<b>Version Information:</b> Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929
</font>
</body>
</html>
<!--
[InvalidOperationException]: Only Web services with a [ScriptService] attri
08-20 11:29:35.761 31958-32374/com.rrtyu.reg D/Retrofit﹕ bute on the class definition can be called from script.
at System.Web.Script.Services.WebServiceData..ctor(Type type, Boolean pageMethods)
at System.Web.Script.Services.WebServiceData.GetWebServiceData(HttpContext context, String virtualPath, Boolean failIfNoData, Boolean pageMethods, Boolean inlineScript)
at System.Web.Script.Services.RestHandler.CreateHandler(HttpContext context)
at System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated)
at System.Web.HttpApplication.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
--><!--
This error page might contain sensitive information because ASP.NET is configured to show verbose error messages using <customErrors mode="Off"/>. Consider using <customErrors mode="On"/> or <customErrors mode="RemoteOnly"/> in production environments.-->
08-20 11:29:35.761 31958-32374/com.reloadapp.reload D/Retrofit﹕ <--- END HTTP (5041-byte body)
答案 0 :(得分:1)
试试这段代码:
JSONObject userDetails = new JSONObject();
try {
userDetails.put("Emailid", emailid.getText().toString());
userDetails.put("Password", password.getText().toString());
userDetails.put("DeviceID", DEVICEID);
userDetails.put("PlatformID", WebUrl.platformid);
userDetails.put("DeviceToken", regid);
userDetails.put("Mobilemodel", Mobilemodel);
userDetails.put("Appversion", "1.2.8.2");
userDetails.put("MobileOS", MobileOs);
Log.v("TAG_USERDEATILS",""+userDetails);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TypedInput body = new TypedByteArray("application/json", userDetails.toString().getBytes("UTF-8"));
serviceClient.getUserInfo(body,
callback);
希望这有帮助!
答案 1 :(得分:-1)
我的问题的解决方案:
像这样更改了serviceclient代码。
public interface ServiceClient {
String SERVICE_ENDPOINT = "https://eload.in/Service/AppServices.asmx";
@FormUrlEncoded
@POST("/Validateuser_v2")
void getUserInfo(
@Field("Emailid") String email,
@Field("Password") String password,
@Field("DeviceID") String deviceid,
@Field("PlatformID") String platformid,
@Field("DeviceToken") String devicetoken,
@Field("Mobilemodel") String mobilemodel,
@Field("Appversion") String appversion,
@Field("MobileOS") String mobileos,
Callback<JsonObject> callback);
}
和活动:
public void onSucceeded() {
ServiceClient serviceClient = ServiceUtil.getServiceClient();
showProgress(R.string.msg_loading);
serviceClient.getUserInfo(emailid.getText().toString(),password.getText().toString(),DEVICEID,WebUrl.platformid,"123",Mobilemodel,"1.2.8.2",MobileOs,
callback);
}
Callback<JsonObject> callback = new Callback<JsonObject>() {
@Override
public void success(JsonObject resultUserInfo, Response response) {
Log.v("TAG_SUCCESS",""+resultUserInfo);
dismissProgress();
}
@Override
public void failure(RetrofitError error) {
dismissProgress();
}
};
void showProgress(int msgID) {
dismissProgress();
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(Login_Activity.this);
}
if (mProgressDialog != null && !mProgressDialog.isShowing()) {
mProgressDialog.setMessage(getResources().getString(msgID));
mProgressDialog.show();
}
}
void dismissProgress() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
}