android kotlin retrofit https post要求jsonパラメータ要求サーバrxjava+retrofit 2

6695 ワード

スレ主は心が疲れて、自分でプロジェクトを書いて游ぶのはすべてgetの要求で、まだpostを游んだことがなくて、またjsonのパラメータが必要です.今補充して、穴はすべて1つ1つ埋めました
その後、リクエストサーバ404が現れ、各種のコードを探し、各種のドキュメントを見て、各種のdebugを歩いて、最後にアドレスに問題があることを発見した.
「404、サーバーの問題じゃないの?」
ここでは1万字を省略します...
 
android kotlin retrofit https post要求jsonパラメータ要求サーバrxjava+retrofit 2
 
1.環境の構成
//コントローラの設定
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
 
//rxjava+retrofit2
compile 'io.reactivex:rxjava:1.3.0'
compile 'io.reactivex:rxandroid:1.2.1'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
 
 
2.ポイントが来た==上コード===
パラメータ:{"service":"login","phoneNum":"17 xxx 2918"}
 
{
    "service": "login",
    "phoneNum": "17xxxxx2918"
}
 
 
val mMMService: MMMService



init {

val retrofit = Retrofit.Builder()

.baseUrl("http://192.168.18x.0x:8080/xxx/";) 

.addConverterFactory(GsonConverterFactory.create())

.addCallAdapterFactory(RxJavaCallAdapterFactory.create())

.build()



mMMService = retrofit.create(MMMService::class.java!!)

}


 
#パッケージjsonデータ方法1#
val parm = JSONObject()

parm.put("service", "login")

parm.put("phoneNum", phone)

val requestBody = RequestBody.create(MediaType.parse("application/json"),

parm.toString())

// val paramBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"),

// parm.toString())//      RequestBody

val phoneCode = mMMService.getPhoneCodes(requestBody)

phoneCode.enqueue(callback)
@Headers("Content-Type: application/json", "Accept: */*")//     

@POST("user.do")

fun getPhoneCodes(@Body route: RequestBody): Call //      RequestBody

 
#パッケージjsonデータ作成beanメソッド2#
class LogonNetBean(private val service: String, private val phoneNum: String){

override fun toString(): String {

return "LogonNetBean(service='$service', phone='$phoneNum')"

}

}

val bean = LogonNetBean("login", phone)

val toString = bean.toString()

Log.e("rexjava", "rexjava::" + toString)

val gson = Gson()

val route = gson.toJson(bean)//  Gson Bean   Json     

val paramBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"),

route)//      RequestBody

val phoneCode = mMMService.getPhoneCodes(paramBody)

phoneCode.enqueue(callback)


 
 
#第2の要求メソッドを追加#
val bean = LogonNetBean("login", phone)

val gson = Gson()

val route = gson.toJson(bean)//  Gson Bean   Json     

val paramBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"),

route)//      RequestBody

val orderObservable: Observable = mMMService.getPhoneCodes(paramBody)

orderObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())

.subscribe({

parserJson(it.status)

}, {

Log.e("rxjava", it.localizedMessage);



}, {

Log.e("rxjava", "onComplete!");

})

 
 
interface MMMService {

@Headers("Content-Type: application/json", "Accept: */*")//     

@POST("user.do")

fun getPhoneCodes(@Body route: RequestBody): Observable //      RequestBody

}


 
 
val parm = JSONObject()

parm.put("service", "login")

parm.put("phoneNum", phone)



var gson = Gson();

val o = gson.fromJson(parm.toString(), Any::class.java)



val phoneCode = mMMService.getPhoneCodes(o)

phoneCode.enqueue(callback)
@Headers("Content-Type: application/json", "Accept: */*")//     

@POST("user.do")

fun getPhoneCodes(@Body route: Any): Call //      RequestBody

 
 
 
val orderObservable: Observable = mMMService.getPhoneCodes("registerNote", phone)

orderObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())

.subscribe({

parserJson(it.data)

}, {

Log.e("rxjava", it.localizedMessage);



}, {

Log.e("rxjava", "onComplete!");

})


 
@POST("user.do")

@FormUrlEncoded

fun getPhoneCodes(@Field("service") type: String,

@Field("phoneNum") version: String): Observable

 
 
interface MMMService {



//    

@GET("home")

// fun getHomeInfo(): Call

fun getHomeInfo(): Observable



@Headers("Content-Type: application/json", "Accept: */*")//     

@POST("user.do")

fun getPhoneCode(@Body route: RequestBody): Call //      RequestBody







}

 
 
abstract class NetPresenter(val activity: AppCompatActivity) {



val mMMService: MMMService



init {

val retrofit = Retrofit.Builder()

.baseUrl("http://192.168.18x.0x:8080/xxx/";) 

.addConverterFactory(GsonConverterFactory.create())

.addCallAdapterFactory(RxJavaCallAdapterFactory.create())

.build()



mMMService = retrofit.create(MMMService::class.java!!)

}





val callback = object : Callback {

override fun onResponse(call: Call?, response: Response?) {

Log.e("rxjava", "   code::" + response?.code())

if (response == null) {

Log.e("rxjava", "         ")

} else {

if (response.isSuccessful) {

val body = response.body()

if (body.status == 0 || body.status == 203) {

val data = body.data

Log.e("rxjava", "data::" + data.toString());

parserJson(data, body.status)

} else {

Log.e("rxjava", "errormsg::" + body.errorMsg);

activity.toast("${body.status} :${body.errorMsg}")

}

} else {

Log.e("rxjava", "       ")

}

}

}





override fun onFailure(call: Call?, t: Throwable?) {

//       

Log.e("home", "        ")



}



}





abstract fun parserJson(data: ResponseInfo.Data?, status: Int)