<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[בעיה בRetrofit באנדרואיד (kotlin)]]></title><description><![CDATA[<p dir="rtl">אני כותב אפליקציית אנדרואיד שמתקשרת עם שרת api, האפליקציה שולחת בקשות עם פרמטרים, והשרת מגיב בjson וכאן משום מה הRetrofit נתקע ומחזיר שגיאה <code>Expected a string but was BEGIN_OBJECT $ at line 1 column 2 path</code> מחיפוש בגוגל מצאתי שזה בעיה מוכרת, אבל לא הצלחתי ליישם אף אחד מהפתרונות שהובאו <a href="https://stackoverflow.com/questions/48296987/retrofit-expected-a-string-but-was-begin-object-at-line-1-column-2-path" target="_blank" rel="noopener noreferrer nofollow ugc">כאן</a> ו<a href="https://stackoverflow.com/questions/32422737/java-lang-illegalstateexception-expected-a-string-but-was-begin-object-at-line" target="_blank" rel="noopener noreferrer nofollow ugc">כאן</a>,</p>
<p dir="rtl">הגדרתי בקובץ <code>ApiService.kt</code></p>
<pre><code>package com.abaye.myapplication

import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Url

interface ApiService {

    @GET
    fun fetchData(@Url url: String): Call&lt;String&gt;
}
</code></pre>
<p dir="rtl">ובקובץ <code>RetrofitClient.kt</code></p>
<pre><code>package com.abaye.myapplication

import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

object RetrofitClient {

    private const val BASE_URL = "https://api.abaye.co/"

    val apiService: ApiService by lazy {
        Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(ApiService::class.java)
    }
}
</code></pre>
<p dir="rtl">וככה אני משתמש</p>
<pre><code>    private fun login(value1: String, callback: LoginCallback) {
        val url = "pter?value1=$value1"
        val call = RetrofitClient.apiService.fetchData(url)

        call.enqueue(object : Callback&lt;String&gt; {
            override fun onResponse(call: Call&lt;String&gt;, response: Response&lt;String&gt;) {
                if (response.isSuccessful) {
                    val result = response.body()
                    showTextBubble(result.toString())
                } else {
                    callback.onFailure("Error: ${response.code()}")
                }
            }

            override fun onFailure(call: Call&lt;String&gt;, t: Throwable) {
                showTextBubble("Error: ${t.message}")
                binding.textView2.setText(t.message.toString())
                callback.onFailure("Error: ${t.message}")
            }
        })
    }
</code></pre>
<p dir="rtl">והonFailure מופעל וזורק את השגיאה לעיל.</p>
<p dir="rtl">יצויין כי תגובת שרת הAPI תקינה לגמרי, וכן כשחוזר טקסט רגיל זה עובד פיקס.</p>
]]></description><link>https://tchumim.com/topic/15983/בעיה-בretrofit-באנדרואיד-kotlin</link><generator>RSS for Node</generator><lastBuildDate>Mon, 16 Nov 2026 15:50:09 GMT</lastBuildDate><atom:link href="https://tchumim.com/topic/15983.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 19 Jan 2024 04:50:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to בעיה בRetrofit באנדרואיד (kotlin) on Sun, 21 Jan 2024 19:29:56 GMT]]></title><description><![CDATA[<p dir="rtl"><a class="plugin-mentions-user plugin-mentions-a" href="/user/%D7%90%D7%91%D7%99%D7%99" aria-label="Profile: אביי">@<bdi>אביי</bdi></a> כתב ב<a href="/post/160346">בעיה בRetrofit באנדרואיד (kotlin)</a>:</p>
<blockquote>
<p dir="rtl">ניסיתי גם לשנות את הtype של הCall אבל אז הכל השתגע כבר בהידור</p>
</blockquote>
<p dir="rtl">הפיתרון אכן היה לשנות את הטייפ לטייפ כללי ולהוסיף מחלקת נתונים שתטפל בהמרה לפני שהיא חוזרת אלי (היינו למשתנה שמכיל את התשובה שמוחזרת)</p>
<pre><code>data class ServerResponse(
    val status: Boolean,
    val statusAction: String,
    val token: String,
    val user: String,
    val name: String,
    val message: String,
    val lastEntry: String,
    val sumMoney: String
)
</code></pre>
<p dir="rtl">הטעות של היתה בצורת הקריאה למחלקה במקרה הזה, שצריכה להיות ככה</p>
<pre><code>Call&lt;Response&lt;ServerResponse&gt;&gt;
</code></pre>
]]></description><link>https://tchumim.com/post/160377</link><guid isPermaLink="true">https://tchumim.com/post/160377</guid><dc:creator><![CDATA[אביי]]></dc:creator><pubDate>Sun, 21 Jan 2024 19:29:56 GMT</pubDate></item><item><title><![CDATA[Reply to בעיה בRetrofit באנדרואיד (kotlin) on Fri, 19 Jan 2024 08:47:32 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/yossiz" aria-label="Profile: yossiz">@<bdi>yossiz</bdi></a> זה השאלה איך אני יכול לגרום לו לפרשן את זה כמחרוזת בכל מקרה? זה בעצם מה שאני צריך, אבל גרשיים לא עוזרות.. ברגע שכללתי אחד מהתווים הללו זה נתקע לא משנה היכן בדיוק התו היה ממוקם...</p>
<pre><code>:;}{][()
</code></pre>
<p dir="rtl">ניסיתי גם לשנות את הtype של הCall אבל אז הכל השתגע כבר בהידור</p>
]]></description><link>https://tchumim.com/post/160346</link><guid isPermaLink="true">https://tchumim.com/post/160346</guid><dc:creator><![CDATA[אביי]]></dc:creator><pubDate>Fri, 19 Jan 2024 08:47:32 GMT</pubDate></item><item><title><![CDATA[Reply to בעיה בRetrofit באנדרואיד (kotlin) on Fri, 19 Jan 2024 08:23:45 GMT]]></title><description><![CDATA[<p dir="rtl"><a class="plugin-mentions-user plugin-mentions-a" href="/user/%D7%90%D7%91%D7%99%D7%99" aria-label="Profile: אביי">@<bdi>אביי</bdi></a> אין לי מושג בקוטלין ו-retrofit אבל ככה נראה מלמעלה שהבעיה פה:</p>
<pre><code>fun fetchData(@Url url: String): Call&lt;String&gt;
</code></pre>
<p dir="rtl">אתה הגדררת ש-<code>fetchData</code> מחזיר (<code>Call</code> של) <code>string</code> ולכן המפרסר (של gson כנראה?) מצפה לגרשיים, בפועל ה-API מחזיר אובייקט</p>
]]></description><link>https://tchumim.com/post/160345</link><guid isPermaLink="true">https://tchumim.com/post/160345</guid><dc:creator><![CDATA[yossiz]]></dc:creator><pubDate>Fri, 19 Jan 2024 08:23:45 GMT</pubDate></item></channel></rss>